Database Articles

Page 6 of 547

How to display only the keys from nested MongoDB documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 628 Views

To display only the keys from nested MongoDB documents, use the aggregation pipeline with $reduce, $map, and $objectToArray operators to extract and combine all field names from nested objects or arrays. Syntax db.collection.aggregate([ { $project: { keys: { $reduce: { ...

Read More

How to push value with for loop in MongoDB?

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

To push values with a for loop in MongoDB, use the save() method inside a for loop to insert multiple documents iteratively. This approach is useful when you need to create many similar documents programmatically. Syntax for(var i = start; i < end; i++) { db.collection.save({field1: "value", field2: "value"}); } Example Create 6 documents using a for loop ? for(var v = 1; v < 7; v++) { db.demo739.save({Name: "Chris", SubjectName: "MongoDB"}); } WriteResult({ "nInserted" : 1 }) Verify ...

Read More

How to order by timestamp (descending order) in MongoDB

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

To order by timestamp in descending order in MongoDB, use the sort() method with the timestamp field set to -1. This arranges documents from newest to oldest timestamp. Syntax db.collection.find().sort({"timestamp": -1}); Create Sample Data Let us create a collection with timestamp documents ? db.demo737.insertMany([ {"timestamp": new ISODate("2020-04-01")}, {"timestamp": new ISODate("2020-10-31")}, {"timestamp": new ISODate("2020-05-02")} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5ead682157bb72a10bcf065c"), ...

Read More

Difference between "now" and a given date with MongoDB?

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

To calculate the difference between the current date and a given date in MongoDB, use the $subtract operator with new Date() to get the current timestamp and subtract the stored date field. Syntax db.collection.aggregate([ { $project: { "differenceInMilliseconds": { $subtract: [new Date(), "$dateField"] }, ...

Read More

How to move an array of embedded documents up to parent and change key/value with aggregation pipeline?

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

Use $replaceRoot in MongoDB aggregation to move array elements up to the parent level and transform key/value pairs. The $replaceRoot stage replaces the input document with a new document structure. Syntax db.collection.aggregate([ { $replaceRoot: { newRoot: { $mergeObjects: [ ...

Read More

How to add a field with specific datatype (list, object) in an existing MongoDB document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 513 Views

To add fields with specific datatypes like objects and arrays to existing MongoDB documents, use the $set operator with update() or updateOne() methods. Syntax db.collection.updateOne( { filter }, { $set: { "objectField": { key: "value" }, "arrayField": [ { item1 }, { item2 } ] }} ); Sample Data db.demo732.insertMany([ { _id: 1, Language: "English" }, ...

Read More

How to display a specific field in array using $project in MongoDB and ignore other fields?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 925 Views

To display a specific field from an array in MongoDB and ignore other fields, use the $project operator with $unwind. Set unwanted fields to 0 to exclude them from the output. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.field": "value" } }, { $project: { "_id": 0, "arrayField.unwantedField": 0 } } ]); Sample Data db.demo731.insertOne({ "ProductInformation": [ { "ProductId": "Product-1", "ProductPrice": 80 }, ...

Read More

Match MongoDB documents with field value greater than a specific number and fetch them?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 547 Views

To match MongoDB documents with field values greater than a specific number, use the $gt operator in your query. This operator filters documents where the specified field value exceeds the given threshold. Syntax db.collection.find({ "fieldName": { "$gt": value } }); // Using with aggregation pipeline db.collection.aggregate([ { $match: { "fieldName": { "$gt": value } } } ]); Sample Data db.demo730.insertMany([ { "Name": "Chris", "Marks": 33 }, { "Name": "David", "Marks": 89 }, { "Name": "Chris", "Marks": ...

Read More

Mass insertion in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 173 Views

For mass insertion in MongoDB, use the insertMany() method to insert multiple documents into a collection in a single operation. This is more efficient than inserting documents individually using multiple insert() calls. Syntax db.collection.insertMany([ { document1 }, { document2 }, { document3 } ]); Example Let us create a collection with multiple bank customer documents ? db.demo729.insertMany([ { BankName: "HDFC Bank", cardType: "Credit", "CustomerName": [{Name: "Chris", Age: 25}] }, { BankName: "ICICI ...

Read More

Find MongoDB records with Price less than a specific value

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 427 Views

To find MongoDB records with Price less than a specific value, use the $lt (less than) comparison operator. This operator filters documents where the specified field value is less than the given value. Syntax db.collection.find({ "field": { $lt: value } }); Sample Data Let us create a collection with sample price documents ? db.demo728.insertMany([ {Price: 75}, {Price: 59}, {Price: 79}, {Price: 89} ]); { "acknowledged": ...

Read More
Showing 51–60 of 5,468 articles
« Prev 1 4 5 6 7 8 547 Next »
Advertisements