Articles on Trending Technologies

Technical articles with clear explanations and examples

How to work with variables in MongoDB query

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

To use variables in MongoDB queries, work with the var keyword to declare variables and reference them in your queries. This approach makes queries more flexible and reusable. Syntax var variableName = "value"; db.collection.find({"field": variableName}); Sample Data Let us create a collection with documents ? db.demo107.insertMany([ {"Name": "Chris"}, {"Name": "Bob"}, {"Name": "David"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e2ee1b19fd5fd66da214471"), ...

Read More

Insert array where element does not exist else update it (with multiple conditions)?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 349 Views

To insert array elements when they don't exist or update them when they do using multiple conditions, use bulkWrite() with multiple updateOne operations. This allows conditional updates and insertions in a single atomic operation. Syntax db.collection.bulkWrite([ { "updateOne": { "filter": { "field": "value", "array": { "$elemMatch": { conditions } } }, "update": { "$set": { "array.$.field": "newValue" } } ...

Read More

Implement $dateToString on array items with MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 313 Views

To implement $dateToString on array items in MongoDB, use the $map operator inside an aggregation pipeline to transform date fields within array elements into formatted strings. Syntax db.collection.aggregate([ { $project: { "arrayField": { $map: { "input": "$arrayField", ...

Read More

MongoDB Aggregate JSON array field for the matching field of other collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 647 Views

To aggregate JSON array field for matching fields from another collection in MongoDB, use $lookup combined with $unwind to join collections based on array elements, then reconstruct the result using $group. Syntax db.collection1.aggregate([ { $unwind: "$arrayField" }, { $lookup: { from: "collection2", localField: "arrayField.joinField", foreignField: "_id", as: "matchedData" }}, ...

Read More

MongoDB query to implement nor query to fetch documents except a specific document

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 212 Views

To fetch documents except a specific document, use the $nor operator in MongoDB. The $nor operator performs a logical NOR operation, returning documents that do not match any of the conditions in the array. Syntax db.collection.find({ $nor: [ { field1: value1 }, { field2: value2 } ] }); Sample Data Let's create a collection with sample documents ? db.demo100.insertMany([ { "Name": "Chris", "Age": 21 ...

Read More

How to group nested fields in MongoDB aggregation with count value in array?

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

To group nested fields in MongoDB aggregation with count values from arrays, use the $objectToArray operator to convert nested objects into key-value pairs, then use $unwind and $group to aggregate the count values. Syntax db.collection.aggregate([ { $project: { fieldName: { $objectToArray: "$nestedObject" } } }, { $unwind: "$fieldName" }, { $group: { _id: "$fieldName.k", count: { $sum: "$fieldName.v.countField" } } } ]) Sample Data db.demo99.insertMany([ { "Details": { ...

Read More

Increment a property value of an element in array object with MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 567 Views

To increment a property value of an element in a MongoDB array, use the $inc operator combined with the $ positional operator. The $ operator identifies the matched array element, while $inc increases the specified field by a given value. Syntax db.collection.update( { "arrayField.property": "matchValue" }, { $inc: { "arrayField.$.targetProperty": incrementValue } } ); Sample Data Let us create a collection with student details ? db.demo97.insertOne({ "Details": [ { ...

Read More

Find sum of fields inside array in MongoDB?

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

To find sum of fields inside array in MongoDB, use the $sum operator with the aggregation pipeline. The $sum operator can directly reference array fields using dot notation to calculate the total of numeric values. Syntax db.collection.aggregate([ { $project: { "fieldName": 1, "totalSum": { $sum: ...

Read More

How to update array with multiple conditions in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 818 Views

To update array with multiple conditions in MongoDB, use $elemMatch to match array elements with specific criteria, combined with $push and the positional operator $ to add new elements to the matched array element. Syntax db.collection.updateOne( { "arrayField": { "$elemMatch": { "field1": "value1", "field2": "value2" } } }, { "$push": { "arrayField.$.targetField": newValue } } ); Sample Data db.demo94.insertOne({ "Details": [ { ...

Read More

How to convert date to timestamp in MongoDB

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

To convert date to timestamp in MongoDB, use the $toLong operator within an aggregation pipeline. This converts an ISODate object to its Unix timestamp representation in milliseconds. Syntax db.collection.aggregate([ { $addFields: { "timestampField": { $toLong: "$dateField" } } } ]); Sample Data db.demo93.insertMany([ {"UserName": "Chris", "ArrivalDate": new ISODate("2020-10-01")}, {"UserName": ...

Read More
Showing 23321–23330 of 61,297 articles
Advertisements