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
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to move an array of embedded documents up to parent and change key/value with aggregation pipeline?
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 MoreHow to add a field with specific datatype (list, object) in an existing MongoDB document?
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 MoreHow to display a specific field in array using $project in MongoDB and ignore other fields?
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 MoreMatch MongoDB documents with field value greater than a specific number and fetch them?
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 MoreMass insertion in MongoDB
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 MoreFind MongoDB records with Price less than a specific value
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 MoreMongoDB query to search for string like “@email” in the field values
To search for strings containing "@email" in MongoDB field values, use regular expressions with the find() method. The i flag enables case-insensitive matching. Syntax db.collection.find({"fieldName": /pattern/flags}); Sample Data Let us create a collection with documents − db.demo727.insertMany([ {UserId: "John@email.com"}, {UserId: "John@yahoo.com"}, {UserId: "Chris@EMAIL.com"} ]); { "acknowledged" : true, "insertedIds" : [ ObjectId("5eab375f43417811278f5898"), ObjectId("5eab376043417811278f5899"), ObjectId("5eab376143417811278f589a") ...
Read MoreHow can I count the documents in an array based on the value of a specific field?
To count documents in an array based on the value of a specific field, use the $unwind operator to flatten the array, followed by $match to filter, and $group to count the matching elements. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.fieldName": "targetValue" } }, { $group: { _id: null, count: { $sum: 1 } }} ]); Sample Data ...
Read MoreSet filtering conditions for nested array in MongoDB
To set filtering conditions for nested arrays in MongoDB, use the $filter operator combined with $anyElementTrue and $map in an aggregation pipeline. This approach allows you to filter outer array elements based on conditions within deeply nested arrays. Syntax db.collection.aggregate([ { $addFields: { "arrayField": { $filter: { ...
Read MoreHow to count a cursor’s iteration in MongoDB?
To count a cursor's iteration in MongoDB, you need to use custom logic with a while loop along with a find() cursor. This allows you to iterate through documents and count specific conditions during traversal. Syntax var cursor = db.collection.find(); var count = 0; while (cursor.hasNext()) { var document = cursor.next(); // Apply your counting logic here if (condition) { count++; } } Sample Data db.demo724.insertMany([ ...
Read More