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
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
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
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
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
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
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance