Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
MongoDB Articles
Page 110 of 111
How to filter array in subdocument with MongoDB?
To filter elements within an array subdocument in MongoDB, you can use $unwind + $match in an aggregation pipeline, or the more concise $filter operator (MongoDB 3.2+). Sample Data db.filterArray.insertOne({ "L": [{"N": 1}, {"N": 2}, {"N": 3}, {"N": 4}, {"N": 5}] }); Method 1: Using $unwind + $match + $group Unwind the array, filter matching elements, then regroup ? db.filterArray.aggregate([ { $match: { _id: ObjectId("5c6d63f2734e98fc0a434aeb") } }, { $unwind: "$L" }, { $match: { "L.N": { ...
Read MoreGet Random record from MongoDB?
Use the $sample aggregation stage to get random records from a MongoDB collection. The size parameter specifies how many random documents to return. Syntax db.collectionName.aggregate([{ $sample: { size: N } }]); Create Sample Data db.employeeInformation.insertMany([ {"EmployeeId": 1, "EmployeeName": "Maxwell", "EmployeeAge": 26}, {"EmployeeId": 2, "EmployeeName": "David", "EmployeeAge": 25}, {"EmployeeId": 3, "EmployeeName": "Carol", "EmployeeAge": 24}, {"EmployeeId": 4, "EmployeeName": "Bob", "EmployeeAge": 28}, {"EmployeeId": 5, "EmployeeName": "Sam", "EmployeeAge": 27} ]); Get One Random Record ...
Read MoreGet names of all keys in the MongoDB collection?
Since MongoDB documents can have different structures (no fixed schema), getting all key names requires iterating over documents. There are multiple approaches depending on whether you need keys from one document or all documents. Method 1: Using JavaScript Loop (Single Document) Use findOne() to get a document and loop through its keys ? var doc = db.studentGetKeysDemo.findOne(); for (var key in doc) { print(key); } Sample Data db.studentGetKeysDemo.insertOne({ "StudentId": 1, "StudentName": "Larry", "StudentAge": 23, ...
Read MoreHow to query MongoDB with LIKE?
MongoDB does not have a LIKE operator like SQL. Instead, use regular expressions (regex) or the $regex operator for pattern matching. Syntax // Using regex literal (equivalent to SQL LIKE '%value%') db.collection.find({"field": /.*value.*/}) // Using $regex operator db.collection.find({"field": {$regex: "value", $options: "i"}}) Create Sample Data db.employee.insertMany([ {"EmployeeName": "John", "EmployeeSalary": 450000}, {"EmployeeName": "Carol", "EmployeeSalary": 150000}, {"EmployeeName": "James", "EmployeeSalary": 550000}, {"EmployeeName": "Jace", "EmployeeSalary": 670000}, {"EmployeeName": "Larry", "EmployeeSalary": 1000000} ]); Query with "like" ...
Read MoreFind objects between two dates in MongoDB?
Use the $gte (greater than or equal) and $lt (less than) operators with ISODate() to find documents between two dates in MongoDB. Create Sample Data db.order.insertMany([ {"OrderId": 1, "OrderAddress": "US", "OrderDateTime": ISODate("2019-02-19")}, {"OrderId": 2, "OrderAddress": "UK", "OrderDateTime": ISODate("2019-02-26")}, {"OrderId": 3, "OrderAddress": "IN", "OrderDateTime": ISODate("2019-03-05")} ]); Find Between Two Dates Find orders between Feb 10 and Feb 21 ? db.order.find({ "OrderDateTime": { $gte: ISODate("2019-02-10"), ...
Read MoreUpdate MongoDB field using value of another field?
In MongoDB, you can update a field using the value of another field in the same document. There are two main approaches − using the aggregation pipeline with $set (MongoDB 4.2+) or using $addFields with $out for writing results to a collection. Method 1: Update with Aggregation Pipeline (MongoDB 4.2+) Use updateMany() with an aggregation pipeline to set a field based on other fields ? // Create sample data db.studentInformation.insertOne({ "StudentFirstName": "Carol", "StudentLastName": "Taylor" }); // Update: create FullName from FirstName + LastName db.studentInformation.updateMany( ...
Read MoreRetrieve only the queried element in an object array in MongoDB collection?
To retrieve only the matching element from an object array in MongoDB, use the $elemMatch projection operator or the $ positional projection operator. Both return only the first array element that matches the query condition. Create Sample Data db.objectArray.insertMany([ { "Persons": [ {"PersonName": "Adam", "PersonSalary": 25000}, {"PersonName": "Larry", "PersonSalary": 27000} ] ...
Read MoreOpen Source Databases
Open source databases have publicly available source code that anyone can view, study, or modify. They can be relational (SQL) or non-relational (NoSQL), and they significantly reduce database costs compared to proprietary solutions. Why Use Open Source Databases? Database licensing is a major software expense for companies. Open source databases offer a cost-effective alternative − free to use, with community support, and no vendor lock-in. Many also offer commercial support tiers for enterprise needs. Popular Open Source Databases Database Type Key Feature MySQL Relational (SQL) Most widely used open source DB; community ...
Read MoreHow to prepare a Python date object to be inserted into MongoDB?
MongoDB stores dates in ISODate format. PyMongo (the official MongoDB driver for Python) directly supports Python's datetime.datetime objects, which are automatically converted to ISODate when inserted. There are three common ways to prepare date objects for MongoDB. Method 1: Current UTC Timestamp Use datetime.datetime.utcnow() to insert the current UTC time ? import datetime from pymongo import MongoClient client = MongoClient() db = client.test_database result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Date Object inserted") Date Object inserted Method 2: Specific Date Use datetime.datetime(year, month, day, hour, minute) for a fixed date ? ...
Read MoreDifference between RDBMS and MongoDB
RDBMS (Relational Database Management System) stores data in structured tables with rows and columns, using SQL to query databases. MongoDB is a NoSQL document-oriented database that stores data as BSON (Binary JSON) documents with dynamic schemas, using its own query language (MQL). RDBMS RDBMS stores data as entities in tables. It provides multiple layers of information security and uses primary keys to uniquely identify records and foreign keys to define relationships between tables. RDBMS follows the ACID principle (Atomicity, Consistency, Isolation, Durability). Examples include Oracle, SQL Server, and MySQL. MongoDB MongoDB is an open-source NoSQL database ...
Read More