MongoDB Articles

Page 24 of 111

Fetch specific documents with array values in MongoD

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 143 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 492 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 622 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 301 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

MongoDB $unwind to get the count

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 621 Views

The $unwind operator in MongoDB deconstructs an array field from input documents to output a separate document for each array element. Use $unwind with $group and $count in an aggregation pipeline to get counts of array elements. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$groupField", count: { $sum: 1 } } } ]); Sample Data db.demo478.insertMany([ { "Details": { ...

Read More

Get the real document BSON size in MongoDB?

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

You can use Object.bsonsize() to get the real document BSON size in MongoDB. This method prints the BSON size of a document in bytes, which is useful for understanding storage requirements and optimizing document structure. Syntax Object.bsonsize(document) Sample Data Let us create a collection with documents ? db.demo477.insertMany([ {"ClientId": 1, "ClientName": "Chris"}, {"ClientId": 2, "ClientName": "David"}, {"ClientId": 3, "ClientName": "Bob"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

MongoDB query to update all documents matching specific IDs

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 873 Views

To update all documents matching specific IDs in MongoDB, use updateMany() with the $in operator to specify multiple ID values in the filter criteria. Syntax db.collection.updateMany( { _id: { $in: [id1, id2, id3] } }, { $set: { field: "newValue" } } ); Sample Data db.demo476.insertMany([ { _id: 1, "Name": "Chris" }, { _id: 2, "Name": "David" }, { _id: 3, "Name": "Bob" }, { _id: 4, "Name": "Carol" ...

Read More

MongoDB query to check the existence of multiple fields

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

To check the existence of multiple fields in MongoDB, use the $exists operator combined with $and. This query returns documents that contain all specified fields. Syntax db.collection.find({ $and: [ { "field1": { $exists: true } }, { "field2": { $exists: true } }, { "field3": { $exists: true } } ] }); Sample Data db.demo475.insertMany([ { "StudentFirstName": ...

Read More

MongoDB function to return a specific data/value?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 281 Views

To return a specific data/value in MongoDB, use the findOne() method. The findOne() method returns the first document that satisfies the specified query criteria from the collection. Syntax db.collection.findOne(query, projection) Create Sample Data Let us create a collection with documents ? db.demo473.insertMany([ { "_id": ObjectId(), "Name": "Chris", "details": { "X-Coordinate": 10, ...

Read More

Finding documents in MongoDB collection where a field is equal to given integer value?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 307 Views

To find documents where a field is equal to a given integer value, use the find() method with the field name and integer value in the query condition. Syntax db.collection.find({"fieldName": integerValue}); Sample Data Let us create a collection with documents − db.demo472.insertMany([ {"Project_Id": -101, "ProjectName": "Online Customer Tracking"}, {"Project_Id": 101, "ProjectName": "Online Banking System"}, {"Project_Id": 102, "ProjectName": "Online Library System"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More
Showing 231–240 of 1,106 articles
« Prev 1 22 23 24 25 26 111 Next »
Advertisements