MongoDB Articles

Page 70 of 111

How to add a sub-document to sub-document array in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 2K+ Views

To add a sub-document to a sub-document array in MongoDB, use the $push operator. This operator appends a new document to an existing array field within your MongoDB collection. Syntax db.collection.update( { "matchField": "value" }, { "$push": { "arrayField": { newSubDocument } } } ) Sample Data Let us first create a collection with documents ? db.subDocumentToSubDocumentDemo.insertOne( { "_id": 101, "StudentName": "Larry", ...

Read More

Check if a list is not empty in MongoDB?

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

To check if a list is not empty in MongoDB, use the $not operator combined with $size to exclude arrays with zero elements. This returns documents where the array field contains at least one element. Syntax db.collection.find({ "arrayField": { "$not": { "$size": 0 } } }); Sample Data db.checkIfListIsNotEmptyDemo.insertMany([ { "UserFriendGroup": ["John", "David"] }, { "UserFriendGroup": ["Carol"] }, { "UserFriendGroup": [] }, { "UserFriendGroup": [null] }, { "UserFriendGroup": [] ...

Read More

Find data for specific date in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 2K+ Views

To find data for a specific date in MongoDB, use the $gte and $lt operators to create a date range. These operators help filter documents where the date field falls within the specified range. Syntax db.collection.find({ "dateField": { "$gte": new Date("YYYY-MM-DD"), "$lt": new Date("YYYY-MM-DD") } }); Sample Data db.findDataByDateDemo.insertMany([ { "UserName": "John", "UserLoginDate": new ISODate("2019-01-31") }, { "UserName": "Larry", "UserLoginDate": new ...

Read More

Efficient way to remove all entries from MongoDB?

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

MongoDB provides two main methods to remove all entries from a collection: drop() and remove(). The key difference is that drop() deletes the entire collection including indexes, while remove() only deletes documents but preserves the collection structure and indexes. Syntax // Method 1: Drop entire collection (fastest) db.collection.drop() // Method 2: Remove all documents (preserves indexes) db.collection.remove({}) // Method 3: Delete all documents (modern approach) db.collection.deleteMany({}) Method 1: Using drop() (Recommended for Complete Removal) Let us first create a collection with documents and indexes ? db.dropWorkingDemo.createIndex({"FirstName":1}); db.dropWorkingDemo.insertOne({"FirstName":"John"}); ...

Read More

How to select where sum of fields is greater than a value in MongoDB?

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

To select documents where the sum of fields is greater than a specific value in MongoDB, use the $expr operator with $add to sum fields and $gt to compare against a threshold value. Syntax db.collection.find({ $expr: { $gt: [ { $add: ["$field1", "$field2", "$field3"] }, threshold_value ] } }); ...

Read More

How to push new items to an array inside of an object in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 1K+ Views

To push new items to an array inside of an object in MongoDB, use the $push operator combined with the $elemMatch operator to match the specific object, then use the $ positional operator to target the array field. Syntax db.collection.update( { "_id": documentId, "arrayField": { "$elemMatch": { "matchField": "matchValue" } } }, { "$push": { "arrayField.$.targetArray": "newValue" } ...

Read More

Push new key element into subdocument of MongoDB?

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

To add new fields to a subdocument in MongoDB, use the $set operator with dot notation to target the specific subdocument field. Syntax db.collectionName.update( {"_id": ObjectId("yourObjectId")}, {$set: {"outerField.newFieldName": "value"}} ); Sample Data Let us create a collection with a document containing an empty subdocument − db.pushNewKeyDemo.insertOne({ "UserId": 100, "UserDetails": {} }); { "acknowledged": true, "insertedId": ObjectId("5cda58f5b50a6c6dd317adbf") } View the current document − ...

Read More

How do I remove a string from an array in a MongoDB document?

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

To remove a string from an array in MongoDB, use the $pull operator. This operator removes all instances of a value or values that match a specified condition from an existing array. Syntax db.collection.update( { }, { $pull: { : } } ); Create Sample Data Let us first create a collection with documents ? db.removeAStringDemo.insertOne({ "Score": [45, 67, 89, "John", 98, 99, 67] }); { "acknowledged": true, ...

Read More

Find a strict document that contains only a specific field with a fixed length?

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

To find documents containing only specific fields with a fixed length in MongoDB, use the $exists operator combined with $where to check field count. This ensures the document has exactly the required fields and no additional ones. Syntax db.collection.find({ "field1": { $exists: true }, "field2": { $exists: true }, $where: function() { return Object.keys(this).length === expectedCount } }); Sample Data db.veryStrictDocumentDemo.insertMany([ {"StudentFirstName": "John", "StudentLastName": "Doe", "StudentAge": 23}, {"StudentFirstName": "Larry"}, ...

Read More

How to get array from a MongoDB collection?

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

To get an array from a MongoDB collection, you can use the aggregation framework with $unwind and $group operators to extract and collect array elements from nested documents. Syntax db.collection.aggregate([ { "$unwind": "$arrayField" }, { "$unwind": "$arrayField.nestedArray" }, { "$group": { "_id": null, "resultArray": { "$push": "$arrayField.nestedArray.targetField" } ...

Read More
Showing 691–700 of 1,106 articles
« Prev 1 68 69 70 71 72 111 Next »
Advertisements