MongoDB Articles

Page 57 of 111

Increment a property value of an element in array object with MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 566 Views

To increment a property value of an element in a MongoDB array, use the $inc operator combined with the $ positional operator. The $ operator identifies the matched array element, while $inc increases the specified field by a given value. Syntax db.collection.update( { "arrayField.property": "matchValue" }, { $inc: { "arrayField.$.targetProperty": incrementValue } } ); Sample Data Let us create a collection with student details ? db.demo97.insertOne({ "Details": [ { ...

Read More

Find sum of fields inside array in MongoDB?

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

To find sum of fields inside array in MongoDB, use the $sum operator with the aggregation pipeline. The $sum operator can directly reference array fields using dot notation to calculate the total of numeric values. Syntax db.collection.aggregate([ { $project: { "fieldName": 1, "totalSum": { $sum: ...

Read More

How to update array with multiple conditions in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 814 Views

To update array with multiple conditions in MongoDB, use $elemMatch to match array elements with specific criteria, combined with $push and the positional operator $ to add new elements to the matched array element. Syntax db.collection.updateOne( { "arrayField": { "$elemMatch": { "field1": "value1", "field2": "value2" } } }, { "$push": { "arrayField.$.targetField": newValue } } ); Sample Data db.demo94.insertOne({ "Details": [ { ...

Read More

How to convert date to timestamp in MongoDB

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

To convert date to timestamp in MongoDB, use the $toLong operator within an aggregation pipeline. This converts an ISODate object to its Unix timestamp representation in milliseconds. Syntax db.collection.aggregate([ { $addFields: { "timestampField": { $toLong: "$dateField" } } } ]); Sample Data db.demo93.insertMany([ {"UserName": "Chris", "ArrivalDate": new ISODate("2020-10-01")}, {"UserName": ...

Read More

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 814 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 330 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 156 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 894 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
Showing 561–570 of 1,106 articles
« Prev 1 55 56 57 58 59 111 Next »
Advertisements