MongoDB Articles

Page 6 of 111

MongoDB aggregation of elements with similar ids in different documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 722 Views

To aggregate elements with similar ids in different documents, use $group in MongoDB aggregation pipeline. This technique groups documents by id and collects all related data into arrays. Syntax db.collection.aggregate([ { $group: { '_id': { id: "$id", "fieldName": "$fieldName" }, "count": { "$sum": 1 } }}, { $group: { '_id': "$_id.id", "aggregatedData": { ...

Read More

Update in MongoDB and prevent overwrite?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 623 Views

To update a document in MongoDB without overwriting the entire document, use the $set operator to modify specific fields while preserving existing data. This prevents accidental data loss during updates. Syntax db.collection.update( { "field": "matchValue" }, { $set: { "fieldToUpdate": "newValue" } } ); Sample Data db.demo601.insertMany([ { id: 1, userDetails: { userName: ...

Read More

Create a new user and set role in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 542 Views

To create a new user and set their role in MongoDB, use the createUser() method. The userAdminAnyDatabase role provides comprehensive administrative privileges across all databases. Syntax use admin db.createUser( { user: "yourUserName", pwd: "yourPassword", roles: [ { role: "yourRoleName", db: "yourDatabaseName" } ] } ) Example Create a user with the "userAdminAnyDatabase" role ? use admin db.createUser( ...

Read More

How to use custom variable while updating a MongoDB document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 529 Views

To use custom variables while updating MongoDB documents, first declare a variable with var, then reference it in your update() operation. This allows for dynamic updates and better code reusability. Syntax var variableName = yourValue; db.collectionName.update( { filter }, { $set: { fieldName: variableName } } ); Create Sample Data db.demo600.insertMany([ { id: 1, Name: "Robert" }, { id: 2, Name: "Mike" }, { id: 3, Name: "Sam" } ]); ...

Read More

How to subtract values (TotalPrice – Discount) from document field values in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 409 Views

To subtract values from document field values in MongoDB, use the $subtract operator within the aggregation pipeline. This operator performs arithmetic subtraction between numeric field values or expressions. Syntax db.collection.aggregate([ { $project: { newField: { $subtract: ["$field1", "$field2"] } ...

Read More

Find which MongoDB document contains a specific string?

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

To find which document contains a specific string, use $regex along with find(). The regex operator performs pattern matching to locate documents containing the specified text substring. Syntax db.collection.find({ fieldName: { $regex: /pattern/flags } }); Sample Data db.demo597.insertMany([ { "Name": "John Doe" }, { "Name": "John Smith" }, { "Name": "Chris Brown" }, { "Name": "Adam Smith" } ]); { "acknowledged": true, ...

Read More

MongoDB query to update nested document

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

To update a nested document in MongoDB, use the $ positional operator with the $set operator. The $ operator identifies the array element that matches the query condition and allows you to update specific fields within that element. Syntax db.collection.update( {"arrayName.field": "matchValue"}, { $set: { "arrayName.$.field": "newValue" } } ); Create Sample Data Let us create a collection with documents − db.demo595.insertOne({ "Information": [ { "_id": new ObjectId(), "Name": "Chris" }, ...

Read More

MongoDB query to limit the returning values of a field?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 214 Views

To limit the returning values of a field in MongoDB, use the $slice operator in the projection parameter of the find() method. This is particularly useful for limiting array elements returned in query results. Syntax db.collection.find( {}, { "arrayField": { "$slice": numberOfElements } } ); Sample Data db.demo594.insertOne({ id: 1, details: [ { Name: "Chris", Age: 21 }, { Name: "Bob", ...

Read More

Fetch specific multiple documents in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 543 Views

To fetch specific multiple documents in MongoDB, use the $in operator to match documents where a field value equals any value in a specified array. Syntax db.collection.find({ "field": { $in: [value1, value2, value3, ...] } }); Sample Data Let us create a collection with documents − db.demo593.insertMany([ { id: 1, "Name": "Chris" }, { id: 2, "Name": "John" }, { id: 3, "Name": "Bob" }, { id: 4, "Name": "Sam" } ]); ...

Read More

MongoDB query to match documents that contain an array field

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 300 Views

To match documents that contain an array field, use the $elemMatch operator. This operator matches documents where at least one array element meets all specified criteria within a single element. Syntax db.collection.find({ "arrayField": { "$elemMatch": { "field1": "value1", "field2": "value2" } } }); Sample Data Let ...

Read More
Showing 51–60 of 1,106 articles
« Prev 1 4 5 6 7 8 111 Next »
Advertisements