MongoDB Articles

Page 67 of 111

MongoDB query to replace value with aggregation?

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

To replace values in MongoDB using aggregation, use the $project stage with the $literal operator. The $literal operator returns a specified value without interpreting it as an expression, making it perfect for replacing field values with static content. Syntax db.collection.aggregate([ { $project: { fieldName: { $literal: "newValue" }, otherField: 1 } ...

Read More

How to search for string or number in a field with MongoDB?

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

To search for string or number values in a field with MongoDB, use the $in operator with an array containing both the string and numeric versions of the value you want to match. Syntax db.collection.find({ "fieldName": { $in: ["stringValue", numericValue] } }); Sample Data Let us create a collection with documents containing both string and numeric values ? db.searchForStringOrNumberDemo.insertMany([ { "StudentName": "Larry", "StudentDetails": { ...

Read More

MongoDB query to return specific fields from an array?

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

To return specific fields from an array in MongoDB, use the aggregation framework with $project stage. You can extract individual array elements or transform array data into specific output formats. Syntax db.collection.aggregate([ { $project: { fieldName: { $arrayElemAt: ["$arrayField.subField", index] }, "arrayField.subField": 1 } } ]); Sample ...

Read More

How to get the matching document inside an array in MongoDB?

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

To get the matching document inside an array in MongoDB, use the $elemMatch operator. This operator matches documents that contain an array field with at least one element that matches all the specified query criteria. Syntax db.collection.find({ "arrayField": { $elemMatch: { "field1": "value1", "field2": "value2" } } }); ...

Read More

How to add an extra field in a sub document in MongoDB?

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

To add an extra field to a subdocument in MongoDB, use the $set operator with dot notation to target specific elements within an array. Use the array index to specify which subdocument to update. Syntax db.collection.update( { "field": "value" }, { "$set": { "arrayField.index.newField": "newValue" } } ); Sample Data db.addExtraFieldDemo.insertOne({ "_id": 1, "UserName": "Larry", "UserOtherDetails": [ { ...

Read More

How to use $regex in MongoDB?

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

The $regex operator in MongoDB allows you to perform pattern matching on string fields using regular expressions. It's useful for text searches, filtering data based on patterns, and implementing case-insensitive queries. Syntax db.collection.find({ fieldName: { $regex: "pattern", $options: "options" } }); Common options include i for case-insensitive matching, m for multiline, and x for extended syntax. Sample Data db.regularExpressionDemo.insertMany([ {"UserName": "John"}, ...

Read More

Find the exact match in array without using the $elemMatch operator in MongoDB?

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

To find an exact match in an array without using the $elemMatch operator in MongoDB, use the $eq operator to match the entire array structure or query specific array elements directly. Syntax // Match entire array exactly db.collection.find({"arrayField": {$eq: ["value1", "value2"]}}); // Match specific element in array db.collection.find({"arrayField": "specificValue"}); Sample Data db.equalDemo.insertMany([ {_id: 1, "StudentFriendNames": ["John", "Carol", "Sam"]}, {_id: 2, "StudentFriendNames": null}, {_id: 3, "StudentFriendNames": ["Carol"]}, {_id: 4, "StudentFriendNames": ["Sam"]} ]); { "_id" ...

Read More

MongoDB query for Partial Object in an array

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

To query for a partial object in a MongoDB array, use dot notation to match specific fields within array elements or use the $elemMatch operator for more complex conditions. Syntax // Dot notation for single field db.collection.find({"arrayName.fieldName": "value"}); // $elemMatch for multiple conditions db.collection.find({"arrayName": {$elemMatch: {"field1": "value1", "field2": "value2"}}}); Sample Data db.queryForPartialObjectDemo.insertMany([ { "StudentDetails": [ {"StudentId": 1, "StudentName": "Chris"} ] ...

Read More

Get the size of a collection in MongoDB using conditions?

George John
George John
Updated on 15-Mar-2026 251 Views

To get the size of a MongoDB collection using specific conditions, you can filter documents with a query and then calculate the total size of matching documents using Object.bsonSize() within a forEach() loop. Syntax var collectionSize = 0; db.collection.find({condition}).forEach(function(document) { var size = Object.bsonSize(document); collectionSize = collectionSize + size; }); print(collectionSize); Sample Data Let us create a collection with sample documents ? db.mongoDBCollectionSizeDemo.insertMany([ {"Name": "John", "Age": 23}, {"Name": "Chris", "Age": 24}, {"Name": ...

Read More

Find documents that contains specific field in MongoDB?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 228 Views

To find documents that contain a specific field in MongoDB, use the $exists operator. This operator checks whether a field exists in a document, regardless of its value. Syntax db.collection.find({ "fieldName": { $exists: true } }); Sample Data Let us create a collection with sample documents − db.findDocumentContainsSpecificFieldDemo.insertMany([ { "ProductPrices": { "Product1": 10, ...

Read More
Showing 661–670 of 1,106 articles
« Prev 1 65 66 67 68 69 111 Next »
Advertisements