MongoDB Articles

Page 23 of 111

How can I aggregate collection and group by field count in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 720 Views

In MongoDB, use the $group stage with aggregate() to group documents by field values and count occurrences. The $sum operator with value 1 counts documents in each group. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", count: { $sum: 1 } } } ]); Sample Data ...

Read More

How to use $type in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 170 Views

The $type operator in MongoDB selects documents where the value of a specified field matches a particular BSON type. This is useful for filtering documents based on data types rather than values. Syntax db.collection.find({ "field": { $type: "bsonType" } }) Where bsonType can be a string name (like "string", "number", "object") or numeric BSON type code. Sample Data Let's create a collection with mixed data types ? db.demo615.insertMany([ { "Value": 100 }, { "Value": "100" }, { "Value": "300" ...

Read More

What is ({$natural: 1}) in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

The {$natural: 1} is a MongoDB sort option that returns documents in their natural insertion order. When used with {$natural: -1}, it works like LIFO (Last In First Out), showing the most recently inserted document first. Syntax db.collection.find().sort({$natural: 1}); // Insertion order db.collection.find().sort({$natural: -1}); // Reverse insertion order Sample Data Let us create a collection with documents ? db.demo614.insertMany([ {"CountryName": "US"}, {"CountryName": "UK"}, {"CountryName": "AUS"}, {"CountryName": "IND"} ]); { ...

Read More

Implementing String Comparison in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 827 Views

To implement string comparison in MongoDB, use the $strcasecmp operator in aggregation pipelines. It performs case-insensitive comparison of two strings and returns: 1 if first string is "greater than" the second string. 0 if the two strings are equal. -1 if the first string is "less than" the second string. Syntax { $project: { fieldName1: 1, fieldName2: 1, comparisonResult: { $strcasecmp: ["$field1", "$field2"] } ...

Read More

MongoDB query to update array object in index N?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 324 Views

To update an array object at a specific index in MongoDB, use the $ positional operator with dot notation. The $ operator identifies the matched array element, allowing you to update its nested fields. Syntax db.collection.update( {"arrayField.matchingField": "value"}, {$set: {"arrayField.$.nestedField": "newValue"}} ) Sample Data db.demo489.insertOne({ details: [ { id: 101, ...

Read More

How to delete partial data in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 317 Views

To delete partial data in MongoDB, you can combine find() with skip() and limit() methods to target specific documents, then use deleteMany() with the $in operator to remove them efficiently. Syntax var ids = db.collection.find({}, {_id: 1}).skip(N).toArray().map(function(d) { return d._id; }); db.collection.deleteMany({_id: {$in: ids}}); Sample Data db.demo488.insertMany([ {"Name": "Chris"}, {"Name": "David"}, {"Name": "Bob"}, {"Name": "Mike"}, {"Name": "Sam"}, {"Name": "John"}, ...

Read More

Implement a query similar to MySQL Union with MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 623 Views

To implement a query similar to MySQL UNION in MongoDB, use the $lookup aggregation stage to join collections, followed by $group and $project to combine and format results. This approach joins collections based on matching fields rather than simply concatenating rows. Syntax db.collection1.aggregate([ { $lookup: { from: "collection2", localField: "matchField", foreignField: "matchField", as: "joinedData" ...

Read More

How to check empty field in a MongoDB collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 3K+ Views

To check empty field in a MongoDB collection, use $exists along with $eq operator. The $exists ensures the field is present, while $eq matches the empty string value. Syntax db.collection.find({ "fieldName": { "$exists": true, "$eq": "" } }); Create Sample Data Let us create a collection with documents containing empty and non-empty fields ? db.demo485.insertMany([ {"FirstName": "Chris", "LastName": ""}, {"FirstName": ...

Read More

Search array of objects in a MongoDB collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 445 Views

To search array of objects in MongoDB, use the find() method with dot notation to target specific fields within array elements. The find() method selects documents in a collection and returns a cursor to the matched documents. Syntax db.collection.find({ "arrayField.objectProperty": "value" }); // Multiple conditions with $or db.collection.find({ $or: [ {"arrayField.property1": "value1", "arrayField.property2": "value2"}, {"arrayField.property1": "value3", "arrayField.property2": "value4"} ] }); Sample Data Let us create ...

Read More

Set variable value in MongoDB save() method

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

To set a variable value in MongoDB's save() method, use db.collectionName.save(variableName) where variableName is your JavaScript variable containing the document data. Syntax var variableName = { field1: "value1", field2: "value2" }; db.collectionName.save(variableName); Example Let us create a variable with document data and save it to a collection ? Step 1: Create a Variable var Info = { "Name": "David", "CountryName": "US", "ProjectDetails": [ { ...

Read More
Showing 221–230 of 1,106 articles
« Prev 1 21 22 23 24 25 111 Next »
Advertisements