Articles on Trending Technologies

Technical articles with clear explanations and examples

Find records on or after a specific date in MongoDB?

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

To find records on or after a specific date in MongoDB, use the $gte (greater than or equal) operator with ISODate to perform date comparisons. Syntax db.collection.find({ "dateField": { $gte: ISODate("YYYY-MM-DD") } }); Sample Data Let us create a collection with documents containing arrival dates ? db.demo91.insertMany([ { "ArrivalDate": new ISODate("2020-01-10") }, { "ArrivalDate": new ISODate("2019-12-14") }, { "ArrivalDate": new ISODate("2020-01-15") } ]); { "acknowledged": true, ...

Read More

MongoDB query to find documents whose array contains a string that is a substring of a specific word

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 820 Views

To find documents whose array contains a string that is a substring of a specific word, use MongoDB's aggregation pipeline with $match, $expr, and $anyElementTrue operators combined with $indexOfBytes to check substring matches. Syntax db.collection.aggregate([ { $match: { $expr: { $anyElementTrue: { ...

Read More

MongoDB query to get array of nested string?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 333 Views

To get array of nested string in MongoDB, use dot notation in find() to query nested array fields. This allows you to search for documents containing specific string values within nested arrays. Syntax db.collection.find({ "parentArray.nestedField": "searchValue" }); Sample Data db.demo89.insertMany([ { id: 101, Details: [ { Name: "Chris", Marks: 45 }, ...

Read More

Is it possible to use MongoDB field value as a pattern in $regex?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 161 Views

Yes, you can use a MongoDB field value as a pattern in $regex using the $expr operator. However, the most practical approach is using $indexOfCP to check if one field value exists within another field. Syntax db.collection.aggregate([ { "$match": { "$expr": { "$ne": [ ...

Read More

MongoDB query to exclude if id is equal to a document field array value

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 895 Views

To exclude documents where the id field equals any value in a document's array field, use $expr with $not and $in operators. This allows field-to-field comparison within the same document. Syntax db.collection.find({ $expr: { $not: { $in: ["$idField", "$arrayField"] } } }); Sample Data Let us create a collection with documents ? db.collection.insertMany([ ...

Read More

How to match multiple criteria inside an array with MongoDB?

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

To match multiple criteria inside an array in MongoDB, use the $elemMatch operator with either find() or aggregate(). The $elemMatch operator matches documents where at least one array element satisfies all the specified criteria. Syntax db.collection.find({ "arrayField": { "$elemMatch": { "field1": "value1", "field2": "value2" } } }) ...

Read More

MongoDB "$and" operator for subcollection to fetch a document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 511 Views

When working with MongoDB subcollections (arrays of documents), use $elemMatch combined with $in or $and operators to fetch documents that match specific conditions within array elements. Syntax // Using $in for single condition db.collection.find({ "arrayField": { "$elemMatch": { "field1": "value1", "field2": { "$in": ["value2"] } } ...

Read More

How to make a unique field in MongoDB?

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

To make a unique field in MongoDB, use unique: true when creating an index. This ensures that no duplicate values can be inserted into the specified field. Syntax db.collection.createIndex( { "fieldName": 1 }, { unique: true } ); Example Let's create a unique index on the EmployeeName field ? db.demo82.createIndex({"EmployeeName":1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } ...

Read More

Finding a specific item in subdocument with MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 202 Views

To find a specific item in a subdocument in MongoDB, use dot notation to access nested fields within array elements. This allows you to query documents based on values inside subdocuments. Syntax db.collection.find({ "arrayField.nestedField": "value" }); Sample Data db.demo81.insertMany([ { "StudentDetails": [ {"StudentName": "Carol", "StudentSubject": "Java"}, {"StudentName": "David", "StudentSubject": "MongoDB"} ...

Read More

MongoDB query to calculate average

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 597 Views

To calculate the average of numeric values in MongoDB, use the $avg aggregation operator within the $group stage. This operator computes the average of all specified numeric values across documents. Syntax db.collection.aggregate([ { $group: { "_id": null, "averageField": { $avg: "$fieldName" } } } ]); Sample Data ...

Read More
Showing 23331–23340 of 61,297 articles
Advertisements