MongoDB Articles

Page 73 of 111

Decrement only a single value in MongoDB?

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

To decrement only a single value in MongoDB, use the $inc operator with a negative value. This allows you to decrease a numeric field by a specified amount while targeting only one document. Syntax db.collection.update( { field: "matchValue" }, { $inc: { numericField: -decrementValue } } ); Create Sample Data db.decrementingOperationDemo.insertMany([ { "ProductName": "Product-1", "ProductPrice": 756 }, { "ProductName": "Product-2", "ProductPrice": 890 }, { "ProductName": "Product-3", "ProductPrice": 994 }, ...

Read More

Search for documents matching first item in an array with MongoDB?

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

To search for documents matching the first item in an array in MongoDB, use dot notation with index 0 to target the first element directly. This approach allows you to query specific fields within the first array element. Syntax db.collection.find({"arrayName.0.fieldName": "value"}); Sample Data db.matchingFirstItemInTheArrayDemo.insertMany([ { "ClientDetails": [ { "ClientName": "Larry", ...

Read More

How to exclude _id without including other fields using the aggregation framework in MongoDB?

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

To exclude the _id field without explicitly including other fields in MongoDB's aggregation framework, use the $project stage with _id: 0 and selectively include only the fields you need. Syntax db.collection.aggregate([ { $project: { _id: 0, "field1": 1, "field2": 1 } ...

Read More

Concatenate fields in MongoDB?

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

To concatenate fields in MongoDB, use the $concat operator within an aggregation pipeline. This operator combines multiple string values into a single string, with optional separators between them. Syntax db.collection.aggregate([ { $project: { "newField": { $concat: ["$field1", "separator", "$field2"] } ...

Read More

MongoDB query where all array items are less than a specified condition?

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

To query MongoDB documents where all array items are less than a specified condition, use the $not operator combined with $gt. This approach ensures that no element in the array is greater than the specified value. Syntax db.collection.find({ "arrayField": { $not: { $gt: value } } }); Sample Data db.arrayElementsNotGreaterThanDemo.insertMany([ {"Scores": [89, 43, 32, 45]}, {"Scores": [32, 33, 34, 40]}, {"Scores": [45, 56, 66, 69]}, {"Scores": [46, 66, 77, 88]} ]); ...

Read More

MongoDB Query to search for records only in a specific hour?

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

To search for records only in a specific hour in MongoDB, use the $hour operator within an aggregation pipeline. The $hour operator extracts the hour component (0-23) from a date field. Syntax db.collection.aggregate([ { $project: { fieldName: { $hour: "$dateField" } } }, { $match: { fieldName: { $in: [hour1, hour2] } } } ]); Sample Data db.mongoDbSearchForHoursDemo.insertMany([ { "CustomerName": "Larry", "OrderDatetime": new ISODate("2019-01-31 09:45:50") ...

Read More

Update multiple rows in a single MongoDB query?

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

To update multiple rows in a single MongoDB query, use bulk operations with initializeUnorderedBulkOp() method. This allows you to batch multiple update operations together and execute them as one atomic operation. Syntax var bulkOp = db.collection.initializeUnorderedBulkOp(); bulkOp.find({condition1}).updateOne({$set: {field: "value"}}); bulkOp.find({condition2}).updateOne({$set: {field: "value"}}); bulkOp.execute(); Sample Data db.upDateMultipleRowsDemo.insertMany([ {"CustomerName": "John", "CustomerPurchaseAmount": 500}, {"CustomerName": "Chris", "CustomerPurchaseAmount": 700}, {"CustomerName": "David", "CustomerPurchaseAmount": 50}, {"CustomerName": "Larry", "CustomerPurchaseAmount": 1900} ]); { "acknowledged": true, ...

Read More

How to implement MongoDB $or operator

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

The $or operator in MongoDB performs a logical OR operation on an array of expressions and returns documents that match at least one of the specified conditions. Syntax db.collection.find({ $or: [ { "field1": value1 }, { "field2": value2 } ] }); Create Sample Data db.orOperatorDemo.insertMany([ { "StudentNames": ["John", "Carol", "Sam"] }, { "StudentNames": ["Robert", "Chris", "David"] }, ...

Read More

Add MD5 hash value to MongoDB collection?

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

To add MD5 hash values to a MongoDB collection, use the hex_md5() function combined with forEach() to iterate through documents and generate hash values for specific fields like passwords. Syntax db.collection.find().forEach(function(doc) { doc.hashField = hex_md5(doc.sourceField); db.collection.save(doc); }); Sample Data db.addMd5HashValueDemo.insertMany([ {"UserName": "Adam", "UserPassword": "Adam123456"}, {"UserName": "Chris", "UserPassword": "Chris_121#"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5cd6a4c66d78f205348bc619"), ...

Read More

Getting a list of values by using MongoDB $group?

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

To get a list of values using MongoDB, use the $group aggregation stage along with the $push operator. This approach groups documents by a specific field and creates an array of values from another field. Syntax db.collection.aggregate([ { $group: { _id: "$groupingField", arrayField: { $push: "$fieldToCollect" } } } ...

Read More
Showing 721–730 of 1,106 articles
« Prev 1 71 72 73 74 75 111 Next »
Advertisements