MongoDB Articles

Page 74 of 111

How to find documents with exactly the same array entries as in a MongoDB query?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 168 Views

To find documents with exactly the same array entries as specified in a MongoDB query, use the $all operator. This operator matches documents where the array field contains all the specified elements, regardless of order. Syntax db.collection.find({ "arrayField": { "$all": ["element1", "element2", "element3"] } }); Sample Data db.findDocumentExactlySameInArrayDemo.insertMany([ {"TechnicalSubjects": ["C++", "Java", "MongoDB"]}, {"TechnicalSubjects": ["MySQL", "Java", "MongoDB"]}, {"TechnicalSubjects": ["C#", "Python", "MongoDB"]}, {"TechnicalSubjects": ["MySQL", "C", "MongoDB"]} ]); { ...

Read More

Get maximum and minimum value in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 2K+ Views

To get the maximum and minimum values from a collection in MongoDB, use the $max and $min aggregation operators within a $group stage. This approach efficiently processes all documents to find the extreme values in a specified field. Syntax db.collection.aggregate([ { $group: { _id: null, MaximumValue: { $max: "$fieldName" }, ...

Read More

What should be used to implement MySQL LIKE statement in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 166 Views

To implement MySQL LIKE statement functionality in MongoDB, use the $regex operator. This provides pattern matching capabilities similar to SQL's LIKE operator for text searches. Syntax db.collection.find({ "fieldName": { $regex: "pattern" } }); // Alternative syntax db.collection.find({ "fieldName": /pattern/ }); Sample Data db.likeInMongoDBDemo.insertMany([ { "Name": "Sam" }, { "Name": "John" }, { "Name": "Scott" }, { "Name": "Sean" }, { "Name": "Samuel" } ]); { ...

Read More

Merge two array fields in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 767 Views

To merge two array fields in MongoDB, use the $setUnion operator in an aggregation pipeline. This operator combines arrays and automatically removes duplicates while preserving unique values from both arrays. Syntax db.collection.aggregate([ { $project: { "MergedFieldName": { $setUnion: ["$arrayField1", "$arrayField2"] } ...

Read More

Find the count of users who logged in between specific dates with MongoDB

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 398 Views

To find the count of users who logged in between specific dates in MongoDB, use the count() method with $gte and $lt operators to define the date range filter. Syntax db.collection.count({ "dateField": { "$gte": new Date("start-date"), "$lt": new Date("end-date") } }); Sample Data db.findDataByDateDemo.insertMany([ { "UserName": "John", "UserLoginDate": new ISODate("2019-01-31") }, { "UserName": "Larry", "UserLoginDate": new ISODate("2019-02-01") }, ...

Read More

Calculate the average value in a MongoDB document grouping by null?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 387 Views

To calculate the average value across all documents in a MongoDB collection, use the $group operator with _id: null to group all documents together, then apply the $avg accumulator operator. Syntax db.collectionName.aggregate([ { $group: { _id: null, "averageFieldName": { $avg: "$fieldName" } } } ]); Sample Data ...

Read More

Implement MongoDB $concatArrays even when some of the values are null?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 383 Views

Use the $ifNull operator with $concatArrays in the aggregation framework to concatenate arrays even when some fields are null or missing. The $ifNull operator replaces null values with an empty array, allowing concatenation to proceed successfully. Syntax db.collection.aggregate([ { $project: { concatenatedField: { $concatArrays: [ { $ifNull: ["$array1", []] }, { $ifNull: ["$array2", []] } ...

Read More

MongoDB query check if value in array property?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 2K+ Views

To check if a value exists in an array property in MongoDB, you can use the $in operator for exact matches. This operator searches for documents where the array field contains any of the specified values. Syntax db.collection.find({ "arrayField": { $in: ["value1", "value2"] } }); Sample Data Let's create a collection with sample documents ? db.valueInArrayDemo.insertMany([ { "UserName": "John", "UserMessage": ["Hi", "Hello", "Bye"] }, ...

Read More

How do I push elements to an existing array in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 418 Views

To push elements to an existing array in MongoDB, use the $addToSet or $push operators with the update() method. The $addToSet adds elements only if they don't already exist, while $push always adds elements regardless of duplicates. Syntax // Using $addToSet (prevents duplicates) db.collection.update( {query}, { $addToSet: { "arrayField": "newElement" } } ); // Using $push (allows duplicates) db.collection.update( {query}, { $push: { "arrayField": "newElement" } } ); Sample Data Let us create a collection with a ...

Read More

How to print document value in MongoDB shell?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 2K+ Views

To print specific document values in MongoDB shell, use the forEach() method combined with the print() function. This allows you to extract and display individual field values from query results. Syntax db.collection.find(query, projection).forEach(function(document) { print(document.fieldName); }); Sample Data db.printDocumentValueDemo.insertMany([ {"InstructorName": "John Smith"}, {"InstructorName": "Sam Williams"}, {"InstructorName": "David Miller"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5cd6804f7924bb85b3f48950"), ...

Read More
Showing 731–740 of 1,106 articles
« Prev 1 72 73 74 75 76 111 Next »
Advertisements