Articles on Trending Technologies

Technical articles with clear explanations and examples

Extract all the values and display in a single line with MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 745 Views

To extract all values from nested arrays and display them in a single line in MongoDB, use the $unwind operator to flatten arrays and $group with $push to combine all values into one array. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $unwind: "$arrayField.nestedArray" }, { $group: { _id: null, result: { $push: "$arrayField.nestedArray" } } } ]); Sample Data db.demo389.insertOne({ "details": [ { "Name": ["Chris", "David"] }, ...

Read More

MongoDB query to remove element from array as sub property

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 440 Views

To remove an element from an array that exists as a sub-property in MongoDB, use the $pull operator with dot notation to target the nested array field. Syntax db.collection.update( { "matchField": "matchValue" }, { "$pull": { "parentField.arrayField": { "field": "valueToRemove" } } } ); Sample Data Let us first create a collection with documents ? db.demo388.insertOne({ _id: '101', userDetails: { isMarried: false, ...

Read More

MongoDB query to unwind two arrays

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 760 Views

To unwind two arrays in MongoDB, use multiple $unwind stages in an aggregation pipeline. This deconstructs each array field to create separate documents for every combination of array elements. Syntax db.collection.aggregate([ { "$unwind": "$arrayField1" }, { "$unwind": "$arrayField2" }, { "$match": { /* optional filtering */ } } ]) Sample Data db.demo387.insertOne({ "Name": "101", "Details1": [ {Value: 100, Value1: 50, Value2: 40}, ...

Read More

Convert date parts to date in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 378 Views

To convert separate date parts (year, month, day) into a complete date in MongoDB, use the $dateFromParts operator within an aggregation pipeline. This operator combines individual date components into a proper date object. Syntax db.collection.aggregate([ { $project: { dateField: { $dateFromParts: { ...

Read More

Update multiple elements in an array in MongoDB?

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

To update multiple elements in an array in MongoDB, use the $[] all positional operator. This operator modifies all elements in the specified array field that match the update criteria. Syntax db.collection.update( { "matchCondition": "value" }, { $set: { "arrayField.$[]": "newValue" } } ); Sample Data Let us first create a collection with documents ? db.demo385.insertOne({ "ServerLogs": [ { "status": ...

Read More

Display only a single field from all the documents in a MongoDB collection

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 817 Views

To display only a single field from all documents in a MongoDB collection, use projection in the find() method. Set the desired field to 1 to include it, or set unwanted fields to 0 to exclude them. Syntax db.collection.find({}, {fieldName: 1, _id: 0}); // OR db.collection.find({}, {unwantedField1: 0, unwantedField2: 0}); Sample Data db.demo384.insertMany([ {"StudentName": "Chris Brown", "StudentCountryName": "US"}, {"StudentName": "David Miller", "StudentCountryName": "AUS"}, {"StudentName": "John Doe", "StudentCountryName": "UK"} ]); { "acknowledged": true, ...

Read More

MongoDB query to filter only the logs containing the "work" word in the content

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 212 Views

To filter logs containing the word "work" in MongoDB, use the $filter operator with $indexOfBytes to perform case-insensitive substring matching within an aggregation pipeline. Syntax db.collection.aggregate([ { "$addFields": { "arrayField": { "$filter": { "input": "$arrayField", ...

Read More

MongoDB aggregate $slice to get the length of the array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 483 Views

To get the length of an array in MongoDB using aggregation, use the $size operator within a $project stage. The $size operator returns the number of elements in the specified array field. Syntax db.collection.aggregate([ { $project: { "fieldName": { $size: "$arrayField" } } } ]); Sample Data Let us create a collection with documents ? db.demo382.insertOne({ "Name": "David", "details": [ { ...

Read More

How to search an array for values present in another array and output the indexes of values found into a new array in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 668 Views

To search an array for values present in another array and output the indexes of values found into a new array in MongoDB, use the $indexOfArray operator combined with $map within an aggregation pipeline. Syntax db.collection.aggregate([ { "$project": { "Result": { "$map": { ...

Read More

MongoDB $addToSet to add a deep nested array of object?

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

The $addToSet operator adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array. When working with deep nested arrays, combine $addToSet with the positional operator $ to target specific array elements. Syntax db.collection.update( { "parentArray.field": "matchValue" }, { $addToSet: { "parentArray.$.nestedArray": newObject } } ); Sample Data Let us first create a collection with documents − db.demo380.insertOne({ "details": [ { ...

Read More
Showing 23091–23100 of 61,297 articles
Advertisements