Articles on Trending Technologies

Technical articles with clear explanations and examples

Tutorix - AI Tutor

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 431 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 226 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 266 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 275 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 315 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 257 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 508 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 426 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 151 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
Showing 23441–23450 of 61,297 articles
Advertisements