Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
MongoDB Articles
Page 73 of 111
Decrement only a single value in MongoDB?
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 MoreSearch for documents matching first item in an array with MongoDB?
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 MoreHow to exclude _id without including other fields using the aggregation framework in MongoDB?
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 MoreConcatenate fields in MongoDB?
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 MoreMongoDB query where all array items are less than a specified condition?
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 MoreMongoDB Query to search for records only in a specific hour?
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 MoreUpdate multiple rows in a single MongoDB query?
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 MoreHow to implement MongoDB $or operator
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 MoreAdd MD5 hash value to MongoDB collection?
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 MoreGetting a list of values by using MongoDB $group?
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