The $elemMatch operator in MongoDB matches documents that contain an array field with at least one element matching all specified query criteria. It's particularly useful when working with arrays of embedded documents. Syntax db.collection.find( { arrayField: { $elemMatch: { field1: value1, field2: value2 } } } ); Sample Data Let us create a collection with documents − db.demo313.insertMany([ { "_id": 100, "details": [{ "Name": "Chris", "Age": 24 }] }, { "_id": 101, "details": [{ "Name": "David", "Age": 22 }] }, ... Read More
To update multiple documents in MongoDB, use the multi: true option in the update() method. By default, MongoDB updates only the first matching document. Syntax db.collection.update( { "field": "matchValue" }, { $set: { "field": "newValue" } }, { multi: true } ); Sample Data db.demo312.insertMany([ { "FirstName": "Robert" }, { "FirstName": "Bob" }, { "FirstName": "Robert" }, { "FirstName": "David" }, ... Read More
To get the count of a specific value in MongoDB quickly, use the countDocuments() method with a query filter. For optimal performance, create an index on the field you're counting. Syntax db.collection.countDocuments({"field": "value"}); Create Index for Performance First, create an index on the Name field for faster counting ? db.demo311.createIndex({"Name": 1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } Sample Data db.demo311.insertMany([ ... Read More
To retrieve a subset of fields from MongoDB documents, use field projection in the find() method. You can specify which fields to include (1) or exclude (0) from the results using dot notation for nested fields. Syntax db.collection.find( { query }, { "field1": 1, "field2": 0, "nested.field": 1 } ); Sample Data db.demo307.insertMany([ { "ClientId": 101, "ClientDetails": { "ClientFirstName": "Chris", "Age": 34 }, ... Read More
To fetch all documents from a MongoDB collection in a beautified, readable format, use the find() method combined with the pretty() method. The pretty() method formats the JSON output with proper indentation and line breaks. Syntax db.collectionName.find().pretty(); Create Sample Data Let us create a collection with some sample documents ? db.demo306.insertMany([ {"Name": "Robert", "Age": 21}, {"Name": "David", "Age": 23}, {"Name": "Mike", "Age": 25}, {"Name": "Carol", "Age": 26} ]); { ... Read More
To get the child of a collection in MongoDB by a specific key, use the find() method with projection. This allows you to retrieve only specific nested fields from documents that match your query criteria. Syntax db.collection.find( { "field": "value" }, { "nestedObject.key": 1 } ); Sample Data db.demo305.insertMany([ { _id: 101, FirstName: "Chris", details: { ... Read More
Use $or operator to fetch values matching any of multiple conditions and pretty() to format the result with proper indentation and readability. Syntax db.collection.find({ $or: [ { field1: value1 }, { field2: value2 } ] }).pretty(); Sample Data Let us create a collection with documents: db.demo304.insertMany([ { "StudentName": "Chris", "StudentAge": 23 }, { "StudentName": "David", "StudentAge": 22 }, ... Read More
To get distinct FirstName values from documents in MongoDB, use the distinct() method. This method returns an array of unique values for the specified field across all documents in the collection. Syntax db.collection.distinct("fieldName") Sample Data db.demo303.insertMany([ {FirstName:"Chris", LastName:"Brown"}, {FirstName:"John", LastName:"Doe"}, {FirstName:"Chris", LastName:"Smith"}, {FirstName:"John", LastName:"Smith"}, {FirstName:"David", LastName:"Miller"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e4ea0f6f8647eb59e56202f"), ... Read More
Field selection in MongoDB using dot notation allows you to access and filter nested fields within embedded documents or arrays. Use dot notation to specify which fields to include or exclude in query results. Syntax db.collection.find( {"parentField.nestedField": "value"}, {"parentField.specificField": 1/0} ); Sample Data db.demo302.insertMany([ {"Id": 101, "details": [{"Name": "Chris", "Age": 21, "Subject": "MySQL"}]}, {"Id": 102, "details": [{"Name": "Bob", "Age": 23, "Subject": "MongoDB"}]}, {"Id": 103, "details": [{"Name": "David", "Age": 20, "Subject": "Java"}]} ]); ... Read More
To change the order of array elements in MongoDB, you can use JavaScript operations with the forEach() method to swap elements or use aggregation pipeline with array operators for more complex reordering. Syntax // Method 1: Using forEach with JavaScript swap db.collection.find().forEach(function(doc) { // Swap elements using temporary variable var temp = doc.arrayField[index1]; doc.arrayField[index1] = doc.arrayField[index2]; doc.arrayField[index2] = temp; db.collection.update({_id: doc._id}, {$set: {arrayField: doc.arrayField}}); }); // Method 2: Using $set with explicit array db.collection.update( ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance