Database Articles

Page 63 of 547

MongoDB - How to copy rows into a newly created collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 407 Views

To copy rows from one collection to a newly created collection in MongoDB, use the aggregation pipeline with the $out operator. This operator writes the aggregation results to a specified collection. Syntax db.sourceCollection.aggregate([ { $match: {} }, { $out: "destinationCollection" } ]); Sample Data Let us first create a source collection with employee documents ? db.sourceCollection.insertMany([ {"EmployeeName": "Robert", "EmployeeSalary": 15000}, {"EmployeeName": "David", "EmployeeSalary": 25000}, {"EmployeeName": "Mike", "EmployeeSalary": 29000} ]); ...

Read More

Replace value with a string literal during MongoDB aggregation operation

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 394 Views

Use MongoDB $literal operator in aggregation pipelines to replace field values with fixed string constants during projection operations. The $literal operator treats its value as a literal value, preventing MongoDB from interpreting it as a field reference or expression. Syntax db.collection.aggregate([ { $project: { fieldName: { $literal: "string_value" } } } ]); Sample Data Let us first create ...

Read More

How to count and sum a field between 2 dates in MongoDB?

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

To count and sum a field between 2 dates in MongoDB, use the aggregation pipeline with $match to filter documents by date range using $gte and $lte, then $group with $sum to calculate totals. Syntax db.collection.aggregate([ { $match: { dateField: { $gte: ISODate("start-date"), ...

Read More

Build index in the background with MongoDB

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

To create index in the background, use createIndex() method and set "background: true" as the second parameter. Background indexing allows MongoDB to build indexes without blocking database operations. Syntax db.collectionName.createIndex( {"fieldName1": 1, "fieldName2": 1}, {background: true} ); Example Let us create a compound index on StudentName and StudentAge fields in the background ? db.indexCreationDemo.createIndex( {"StudentName": 1, "StudentAge": 1}, {background: true} ); { "createdCollectionAutomatically": true, "numIndexesBefore": ...

Read More

Fix: MongoDB Robomongo: db.data.find(...).collation is not a function?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 153 Views

The collation feature was introduced in MongoDB version 3.4. If you're encountering the error "collation is not a function", you're likely using an older MongoDB version or an outdated client like Robomongo that doesn't support this feature. Check MongoDB Version First, verify your MongoDB version to ensure it's 3.4 or higher ? db.version() 4.0.5 Create Sample Data Let's create a collection with an index that uses collation and insert test documents ? db.collationExample.createIndex( {Value: 1}, {collation: {locale: "en", strength: ...

Read More

Calculating average value per document with sort in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 254 Views

To calculate the average value per document in MongoDB, use the aggregation framework with $addFields and $avg operators. You can combine this with $sort to order results by the calculated average. Syntax db.collection.aggregate([ { $addFields: { "avgField": { $avg: "$arrayField" } } }, { $sort: { "avgField": 1 } } ]); Sample Data db.calculateAverage.insertMany([ { "Value": [10, 20, 80] }, { "Value": [12, 15, 16] }, { "Value": [30, 35, 40] } ]); ...

Read More

Promote subfields to top level in projection without listing all keys in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 219 Views

To promote subfields to top level in projection without listing all keys in MongoDB, use $replaceRoot with $arrayToObject and $objectToArray operators. This technique flattens nested objects by converting them to arrays and reconstructing them at the root level. Syntax db.collection.aggregate([ { "$replaceRoot": { "newRoot": { "$arrayToObject": { ...

Read More

Deleting specific record from an array nested within another array in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 753 Views

To delete specific record from an array nested within another array in MongoDB, use the $pull operator combined with the $ positional operator to target the correct parent array element and remove the matching nested document. Syntax db.collection.update( { "parentArray.field": "matchValue" }, { $pull: { "parentArray.$.nestedArray": { "field": "valueToDelete" } } } ); Sample Data db.demo213.insertOne({ "id": 101, "details1": [ { ...

Read More

Finding a MongoDB document through a word

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 214 Views

To find a MongoDB document through a word, use the find() method with regular expression pattern matching. The /word/i syntax performs case-insensitive search for the specified word within field values. Syntax db.collection.find({ "fieldName": /searchWord/i }); Create Sample Data db.demo212.insertMany([ {"details": [{"Name": "John Doe"}]}, {"details": [{"Name": "Chris Brown"}]}, {"details": [{"Name": "Robert doe"}]} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e3e2c7603d395bdc21346ff"), ...

Read More

How do you test if two external values are equal in a MongoDB criteria object?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 127 Views

To test if two external values are equal in a MongoDB criteria object, you can use the $type operator combined with a JavaScript expression that evaluates the equality condition and returns different BSON type numbers. Syntax db.collection.find({ field: "value", field: { $type: baseNumber + (value1 === value2) } }); Sample Data db.demo211.insertMany([ { id: 101, "Name": "Chris" }, { id: 102, "Name": null } ]); { "acknowledged": true, ...

Read More
Showing 621–630 of 5,468 articles
« Prev 1 61 62 63 64 65 547 Next »
Advertisements