Articles on Trending Technologies

Technical articles with clear explanations and examples

MongoDB query to update array object in index N?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 329 Views

To update an array object at a specific index in MongoDB, use the $ positional operator with dot notation. The $ operator identifies the matched array element, allowing you to update its nested fields. Syntax db.collection.update( {"arrayField.matchingField": "value"}, {$set: {"arrayField.$.nestedField": "newValue"}} ) Sample Data db.demo489.insertOne({ details: [ { id: 101, ...

Read More

How to delete partial data in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 320 Views

To delete partial data in MongoDB, you can combine find() with skip() and limit() methods to target specific documents, then use deleteMany() with the $in operator to remove them efficiently. Syntax var ids = db.collection.find({}, {_id: 1}).skip(N).toArray().map(function(d) { return d._id; }); db.collection.deleteMany({_id: {$in: ids}}); Sample Data db.demo488.insertMany([ {"Name": "Chris"}, {"Name": "David"}, {"Name": "Bob"}, {"Name": "Mike"}, {"Name": "Sam"}, {"Name": "John"}, ...

Read More

Implement a query similar to MySQL Union with MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 632 Views

To implement a query similar to MySQL UNION in MongoDB, use the $lookup aggregation stage to join collections, followed by $group and $project to combine and format results. This approach joins collections based on matching fields rather than simply concatenating rows. Syntax db.collection1.aggregate([ { $lookup: { from: "collection2", localField: "matchField", foreignField: "matchField", as: "joinedData" ...

Read More

How to check empty field in a MongoDB collection?

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

To check empty field in a MongoDB collection, use $exists along with $eq operator. The $exists ensures the field is present, while $eq matches the empty string value. Syntax db.collection.find({ "fieldName": { "$exists": true, "$eq": "" } }); Create Sample Data Let us create a collection with documents containing empty and non-empty fields ? db.demo485.insertMany([ {"FirstName": "Chris", "LastName": ""}, {"FirstName": ...

Read More

Search array of objects in a MongoDB collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 449 Views

To search array of objects in MongoDB, use the find() method with dot notation to target specific fields within array elements. The find() method selects documents in a collection and returns a cursor to the matched documents. Syntax db.collection.find({ "arrayField.objectProperty": "value" }); // Multiple conditions with $or db.collection.find({ $or: [ {"arrayField.property1": "value1", "arrayField.property2": "value2"}, {"arrayField.property1": "value3", "arrayField.property2": "value4"} ] }); Sample Data Let us create ...

Read More

Set variable value in MongoDB save() method

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

To set a variable value in MongoDB's save() method, use db.collectionName.save(variableName) where variableName is your JavaScript variable containing the document data. Syntax var variableName = { field1: "value1", field2: "value2" }; db.collectionName.save(variableName); Example Let us create a variable with document data and save it to a collection ? Step 1: Create a Variable var Info = { "Name": "David", "CountryName": "US", "ProjectDetails": [ { ...

Read More

Fetch specific documents with array values in MongoD

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 146 Views

To fetch specific documents with array values in MongoDB, use the limit() method combined with toArray(). The toArray() method returns an array containing all documents from a cursor, while limit() restricts the number of documents returned. Syntax db.collection.find().limit(numberOfDocuments).toArray() Create Sample Data Let us create a collection with documents containing array values ? db.demo482.insertMany([ {_id:1, "StudentInformation":[{"Name":"Chris", "Age":21}]}, {_id:2, "StudentInformation":[{"Name":"Bob", "Age":23}]}, {_id:3, "StudentInformation":[{"Name":"David", "Age":20}]} ]); { "acknowledged" : true, "insertedIds" : ...

Read More

How to use save() correctly in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 497 Views

The save() method in MongoDB is used to insert a new document or update an existing document based on whether the document contains an _id field. If no _id is provided, it inserts a new document. If an _id exists, it updates the existing document. Syntax db.collection.save(document) Create Sample Data Let us create a collection with documents using save() ? db.demo481.save({"FirstName":"Chris", "LastName":"Brown"}); db.demo481.save({"FirstName":"David", "LastName":"Miller"}); db.demo481.save({"FirstName":"John", "LastName":"Doe"}); db.demo481.save({"FirstName":"John", "LastName":"Smith"}); WriteResult({ "nInserted" : 1 }) WriteResult({ "nInserted" : 1 }) WriteResult({ "nInserted" : 1 }) WriteResult({ "nInserted" : 1 }) ...

Read More

Format date value in MongoDB shell?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 626 Views

To format date values in MongoDB shell, use the $dateToString operator within an aggregation pipeline. This operator converts ISODate values into custom formatted strings according to your specified format pattern. Syntax { "$dateToString": { "format": "formatString", "date": "$dateField" } } Sample Data db.demo480.insertMany([ { "id": 1, "DueDate": new ISODate("2020-01-10") }, { "id": 1, "DueDate": new ISODate("2017-12-21") }, ...

Read More

MongoDB query to add timestamp only if it is not present

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 307 Views

To add a timestamp field only if it is not already present in MongoDB documents, use the $exists operator with update() and set multi: true to update multiple documents at once. Syntax db.collection.update( { "fieldName": { $exists: false } }, { $set: { "fieldName": new Date() } }, { multi: true } ); Sample Data db.demo479.insertMany([ { "DueDate": ISODate("2020-01-10"), "Name": "Chris" }, { "Name": "David" }, { "DueDate": ISODate("2019-12-31"), ...

Read More
Showing 22991–23000 of 61,297 articles
Advertisements