MongoDB Articles

Page 68 of 111

Getting only the first item for an array property in MongoDB?

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

To get only the first item from an array property in MongoDB, use the $slice operator in the projection stage. This operator limits the number of array elements returned in the query result. Syntax db.collection.find( { query }, { arrayField: { $slice: 1 } } ); Sample Data db.gettingFirstItemInArrayDemo.insertOne({ "UserId": 101, "UserName": "Carol", "UserOtherDetails": [ {"UserFriendName": "Sam"}, ...

Read More

How to limit the amount of characters returned from a field in a MongoDB query?

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

To limit the amount of characters returned from a field in a MongoDB query, use the $substr operator within an aggregation pipeline. This operator extracts a substring from a string field based on starting position and character count. Syntax db.collection.aggregate([ { $project: { fieldName: { $substr: ["$originalField", startIndex, numberOfCharacters] } } } ]); Create Sample Data db.limitTheNumberOfCharactersDemo.insertMany([ ...

Read More

How to pop a single value in MongoDB?

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

To pop a single value in MongoDB, use the $pop operator to remove the first or last element from an array field. The $pop operator takes a value of 1 to remove the last element or -1 to remove the first element. Syntax db.collection.updateOne( { query }, { $pop: { "arrayField": 1 } } // Remove last element ); db.collection.updateOne( { query }, { $pop: { "arrayField": -1 } } // Remove first element ); ...

Read More

How to print results of script in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 1K+ Views

To print results of a script in MongoDB, use the printjson() method along with cursor iteration. This allows you to display query results in a formatted JSON structure, which is particularly useful when working with scripts or loops. Syntax var cursor = db.collection.find(); while (cursor.hasNext()) { printjson(cursor.next()); } Create Sample Data Let us first create a collection with documents ? db.printResultScriptDemo.insertMany([ {"StudentName": "John", "StudentAge": 21}, {"StudentName": "Carol", "StudentAge": 20}, {"StudentName": "David", "StudentAge": 19} ]); ...

Read More

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
Showing 671–680 of 1,106 articles
« Prev 1 66 67 68 69 70 111 Next »
Advertisements