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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Updating a set of documents from a list of key value pairs in MongoDB
To update multiple documents from a list of key-value pairs in MongoDB, use bulk operations with initializeUnorderedBulkOp() to efficiently process multiple updates in a single operation. Syntax var bulkUpdateValue = [{"_id": "key1", "field": "value1"}, {"_id": "key2", "field": "value2"}]; var bulkUpdate = db.collection.initializeUnorderedBulkOp(); for (var i = 0; i < bulkUpdateValue.length; i++){ var updateItem = bulkUpdateValue[i]; bulkUpdate.find({_id: updateItem._id}).update({$set: {field: updateItem.field}}); } bulkUpdate.execute(); Sample Data db.demo227.insertMany([ {"_id": "101", "Name": "Chris"}, {"_id": "102", "Name": "Bob"} ]); { ...
Read MoreMongoDB query to display all the values excluding the id?
To exclude the _id field from MongoDB query results, use the $project operator in an aggregation pipeline. The $project stage allows you to include or exclude fields, suppress the _id field, and rename existing fields. Syntax db.collection.aggregate([ { $project: { _id: 0, field1: 1, field2: 1 } } ]); Sample Data db.demo226.insertMany([ { "Name": "Chris", "Age": 21 }, { "Name": "Bob", "Age": 20 }, { "Name": "David", "Age": 22 } ]); { ...
Read MoreHow to compare multiple properties in MongoDB?
To compare multiple properties in MongoDB, you can use the $where operator or the $expr operator. The $where allows JavaScript expressions to compare fields, while $expr uses aggregation expressions for better performance. Syntax // Using $where (JavaScript expression) db.collection.find({ $where: "this.field1 > this.field2" }); // Using $expr (Aggregation expression - Recommended) db.collection.find({ $expr: { $gt: ["$field1", "$field2"] } }); Sample Data db.demo223.insertMany([ {"Scores": [56, 78]}, {"Scores": [88, 45]}, {"Scores": [98, 79]} ]); { ...
Read MoreMongoDB query to add new array element in document
To add new array element in a MongoDB document, use the $ positional operator along with $set in an update operation. This allows you to add a new field to a specific element within an array. Syntax db.collection.update( {"arrayField.existingField": "matchValue"}, {"$set": {"arrayField.$.newField": "newValue"}} ); Sample Data db.demo222.insertOne({ "details": [ { "StudentName": "Chris", ...
Read MoreMongoDB regular expression to fetch record with specific name "John", instead of "john
To fetch records with a specific case-sensitive name using MongoDB regular expressions, use the exact pattern match syntax /searchWord/. By default, MongoDB regex is case-sensitive, so /John/ will match "John" but not "john". Syntax db.collection.find({"field": /exactPattern/}); Sample Data db.demo221.insertMany([ {"Details": {"StudentName": "Chris", "StudentAge": 21}}, {"Details": {"StudentName": "John", "StudentAge": 20}}, {"Details": {"StudentName": "Bob", "StudentAge": 22}}, {"Details": {"StudentName": "john", "StudentAge": 24}} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreGet all embedded documents with "isMarried" status in a MongoDB collection
To get all embedded documents with specific fields in MongoDB, use the $project aggregation operator. This allows you to extract and display specific fields from embedded documents. Syntax db.collection.aggregate([ { $project: { "fieldName": "$embeddedDocument.fieldName", "anotherField": "$embeddedDocument.anotherField" } } ]); Sample Data db.demo220.insertMany([ ...
Read MoreSearching a specific domain name from URL records in MongoDB?
To search for a specific domain name from URL records in MongoDB, use the regex pattern with the /i flag for case-insensitive matching. This allows you to find URLs containing specific domain names regardless of their capitalization. Syntax db.collection.find({ "fieldName": /domainName/i }); Sample Data Let us create a collection with URL documents − db.demo219.insertMany([ {"details": {"WebsiteURL": "www.EXAMPLE.com"}}, {"details": {"WebsiteURL": "www.gmail.com"}}, {"details": {"WebsiteURL": "www.example.com"}} ]); { "acknowledged": true, ...
Read MoreUsing aggregation pipeline to fetch records in MongoDB
The MongoDB aggregation pipeline processes documents through multiple stages, where each stage transforms the documents as they pass through the pipeline. It's a powerful framework for data analysis and transformation operations. Syntax db.collection.aggregate([ { $stage1: { /* stage operations */ } }, { $stage2: { /* stage operations */ } }, { $stageN: { /* stage operations */ } } ]) Sample Data db.demo218.insertMany([ {"Name": "Chris", "Branch": "CS", "Marks": [65, 78, 36, 90]}, ...
Read MoreHow to trim spaces from field in MongoDB query?
To trim spaces from field in MongoDB, use the $trim operator within an aggregation pipeline. The $trim operator removes whitespace characters from the beginning and end of a string. Syntax db.collection.aggregate([ { $project: { fieldName: { $trim: { input: "$sourceField" } } }} ]) Sample Data db.demo217.insertMany([ {"FullName": " Chris Brown"}, {"FullName": " David Miller"}, {"FullName": " John Doe"} ]); { ...
Read MoreProjection of one column in MongoDB?
In MongoDB, projection allows you to display only specific fields from documents instead of returning all fields. To project a single column (field), use the find() method with a projection document that specifies which fields to include or exclude. Syntax db.collection.find({}, {fieldToShow: 1, _id: 0}); Or exclude unwanted fields: db.collection.find({}, {fieldToHide: 0, anotherField: 0}); Sample Data Let us first create a collection with documents − db.demo216.insertMany([ {"ClientName": "John", "ClientAge": 34}, {"ClientName": "Bob", "ClientAge": 32}, {"ClientName": "Mike", ...
Read More