Database Articles

Page 71 of 547

Group all documents with common fields in MongoDB?

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

To group all documents with common fields in MongoDB, use the $group aggregation stage with $addToSet operator. This accumulates unique values from documents that share the same field value. Syntax db.collection.aggregate([ { $group: { _id: "$fieldToGroupBy", "groupedField": { $addToSet: "$fieldToAccumulate" } } } ]); Sample Data ...

Read More

Compare multiple properties in MongoDB?

George John
George John
Updated on 15-Mar-2026 221 Views

To compare multiple properties in MongoDB, use the $where operator which allows JavaScript expressions to evaluate conditions between different fields or array elements within the same document. Syntax db.collection.find({ $where: "this.field1 > this.field2" }); Sample Data Let us create a collection with sample documents − db.comparingMultiplePropertiesDemo.insertOne({ "Values": [10, 70, 60] }); { "acknowledged": true, "insertedId": ObjectId("5cf228fcb64a577be5a2bc0a") } Display the document to verify insertion − db.comparingMultiplePropertiesDemo.find().pretty(); { ...

Read More

Delete specific record from an array nested within another array in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 249 Views

To delete a specific record from an array nested within another array in MongoDB, use the $pull operator combined with the $ positional operator to target elements in nested arrays. Syntax db.collection.update( {"parentArray.field": "value"}, { $pull: { "parentArray.$.nestedArray": { "field": "matchValue" } } } ); Sample Data Let us first create a collection with nested array documents ? db.deletingSpecificRecordDemo.insertOne({ "StudentDetails": [ { ...

Read More

Specify return format in MongoDB to return the values as an array?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 271 Views

To specify return format in MongoDB and return values as an array, use the aggregation pipeline with the $group stage and $addToSet operator to collect distinct values into an array. Syntax db.collection.aggregate([ { "$group": { "_id": null, "fieldName": { "$addToSet": "$fieldPath" } } }, { ...

Read More

Update key value where another key equals some value in MongoDB?

George John
George John
Updated on 15-Mar-2026 289 Views

To update a key value where another key equals a specific value in MongoDB arrays, use $elemMatch to match the array element and $set with the positional operator $ to update the matched element. Syntax db.collection.update( { "arrayField": { "$elemMatch": { "matchKey": "matchValue" } } }, { "$set": { "arrayField.$.updateKey": "newValue" } } ); Sample Data Let us first create a collection with documents − db.keyValueDemo.insertOne({ "_id": new ObjectId(), "CustomerDetails": [ ...

Read More

Is it possible to return a list of specific values from a MongoDB query?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 243 Views

Yes, you can return a list of specific values from a MongoDB query using the map() method or projection. The map() method transforms query results into an array of specific field values. Syntax db.collection.find().map(function(document){ return document.fieldName; }); Sample Data db.listOfSpecificValuesDemo.insertMany([ {"StudentName": "John"}, {"StudentName": "Chris"}, {"StudentName": "Robert"}, {"StudentName": "David"} ]); { "acknowledged": true, "insertedIds": { ...

Read More

MongoDB query to select one field if the other is null and the first field if both are not null?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 491 Views

In MongoDB, to select one field when another is null and the first field when both are not null, use the $ifNull operator in an aggregation pipeline. This operator returns the first non-null value from the provided expressions. Syntax db.collection.aggregate([ { $project: { "fieldName": { "$ifNull": [ "$primaryField", "$fallbackField" ] } } } ]); Sample Data Let us ...

Read More

Limit number of values in a field using MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 409 Views

To limit the number of values in a field using MongoDB, use the $slice projection operator. This operator allows you to return only a specified number of elements from an array field. Syntax db.collection.find( {}, { "arrayField": { "$slice": numberOfElements } } ); Sample Data Let us first create a collection with documents ? db.numberOfValuesDemo.insertOne({ "Values": [100, 200, 300, 900, 1000, 98] }); { "acknowledged": true, "insertedId": ObjectId("5cefb736ef71edecf6a1f6ab") } ...

Read More

Display the last two values from field with MongoDB

George John
George John
Updated on 15-Mar-2026 142 Views

To display the last two values from a field in MongoDB, use the $slice operator with a negative value in the projection. The negative value indicates retrieving elements from the end of the array. Syntax db.collection.find({}, { "fieldName": { "$slice": -N } }); Sample Data Let us first create a collection with documents ? db.numberOfValuesDemo.insertOne({ "Values": [100, 200, 300, 900, 1000, 98] }); { "acknowledged": true, "insertedId": ObjectId("5cefb736ef71edecf6a1f6ab") } Display all documents from a collection ...

Read More

MongoDB query to get last inserted document?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 14K+ Views

To get the last inserted document in MongoDB, use sort() with _id in descending order along with limit(1). Since ObjectId contains a timestamp, sorting by _id in descending order retrieves the most recently inserted document. Syntax db.collection.find().sort({_id: -1}).limit(1); Sample Data Let us first create a collection with documents ? db.getLastInsertedDocument.insertMany([ {"Name": "John"}, {"Name": "Chris"}, {"Name": "Robert"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More
Showing 701–710 of 5,468 articles
« Prev 1 69 70 71 72 73 547 Next »
Advertisements