Articles on Trending Technologies

Technical articles with clear explanations and examples

Tutorix - AI Tutor

Add a field to an embedded document in an array in MongoDB?

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

To add a field to an embedded document in an array in MongoDB, use the $set operator combined with the $ positional operator and $elemMatch to target the specific array element. Syntax db.collection.update( { "arrayField": { $elemMatch: { "matchField": "value" } } }, { $set: { "arrayField.$.newField": "newValue" } } ); Sample Data Let us create a collection with documents ? db.addAFieldDemo.insertOne({ "ClientName": "Larry", "ClientCountryName": "US", "ClientOtherDetails": [ ...

Read More

Calculate average of ratings in array and then include the field to original document in MongoDB?

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

To calculate the average of ratings in an array and include this field in the original document in MongoDB, use the $addFields stage with $avg operator in an aggregation pipeline. This adds a computed field without modifying the original document structure. Syntax db.collection.aggregate([ { $addFields: { averageField: { $avg: "$arrayField.numericField" } } } ]); Sample Data db.averageOfRatingsInArrayDemo.insertOne({ ...

Read More

Match element in array of MongoDB?

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

To match elements in arrays in MongoDB, you can use the $or operator to search for documents where any array element matches one or more conditions. This approach is useful when you want to find documents containing arrays with specific field values. Syntax db.collection.find({ $or: [ {"arrayField.subfield1": "value1"}, {"arrayField.subfield2": "value2"} ] }).limit(1); Sample Data db.matchElementInArrayDemo.insertMany([ { "StudentName": ...

Read More

How to get a specific object from array of objects inside specific MongoDB document?

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

To get a specific object from array of objects inside a MongoDB document, use the positional operator $ in the projection to return only the matched array element. Syntax db.collection.find( { "arrayField.subField": "matchValue" }, { "_id": 0, "arrayField.$": 1 } ); Sample Data Let us create a collection with documents − db.getASpecificObjectDemo.insertOne({ _id: 1, "CustomerName": "Larry", "CustomerDetails": { "CustomerPurchaseDescription": [ ...

Read More

Query to retrieve multiple items in an array in MongoDB?

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

To retrieve multiple items in an array in MongoDB, use the aggregation framework with $unwind, $match, and $group operators to filter and reconstruct array elements based on specific criteria. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.field": { $in: [values] } } }, { $group: { "_id": "$_id", "arrayField": { $push: "$arrayField" } } } ]); Sample Data db.retrieveMultipleDemo.insertOne({ "UserDetails": [ { "_id": "101", "UserName": "John", "UserAge": ...

Read More

How to search for documents based on the value of adding two properties in MongoDB?

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

To search for documents based on the value of adding two properties in MongoDB, use the aggregation framework with $add operator to calculate the sum and $match to filter results based on the calculated value. Syntax db.collection.aggregate([ { $project: { totalValue: { $add: ["$field1", "$field2"] } } }, { $match: { totalValue: { $operator: value } } } ]); Sample Data db.searchDocumentsDemo.insertMany([ {"Value1": 100, "Value2": 560}, {"Value1": 300, "Value2": 150}, {"Value1": 400, "Value2": ...

Read More

Insert MongoDB document field only when it's missing?

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

To insert a MongoDB document field only when it's missing, use the $exists operator in the query condition to target documents where the field doesn't exist, then apply $set to add the field only to those documents. Syntax db.collection.update( { "fieldName": { "$exists": false } }, { "$set": { "fieldName": "value" } }, { "multi": true } ); Sample Data db.missingDocumentDemo.insertMany([ {"StudentFirstName": "Adam", "StudentLastName": "Smith"}, {"StudentFirstName": "Carol", "StudentLastName": "Taylor"}, ...

Read More

Find all documents that have two specific id's in an array of objects in MongoDB?

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

To find all documents that have two specific id's in an array of objects in MongoDB, use the $and operator with dot notation to match multiple values within the same array field. Syntax db.collection.find({ $and: [ { "arrayField.property": value1 }, { "arrayField.property": value2 } ] }); Sample Data db.twoSpecificIdsDemo.insertMany([ { PlayerId: 1, ...

Read More

MongoDB pull with positional operator?

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

The $pull operator combined with the positional operator ($) in MongoDB allows you to remove specific values from arrays within nested documents. Use $elemMatch to identify the target document and $ to reference the matched array element. Syntax db.collection.update( { "arrayField": { "$elemMatch": { "field": "matchValue" ...

Read More

Search a sub-field on MongoDB?

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

To search a sub-field in MongoDB, use dot notation with the field path enclosed in double quotes. This allows you to query nested fields within embedded documents. Syntax db.collection.find({"parentField.subField": "value"}); Sample Data db.searchSubFieldDemo.insertMany([ { "UserDetails": { "UserEmailId": "John123@gmail.com", "UserAge": 21 } }, ...

Read More
Showing 23521–23530 of 61,297 articles
Advertisements