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
MongoDB Articles
Page 55 of 111
Searching 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 MoreHow to insert a boolean field in MongoDB?
To insert a boolean field in MongoDB, use the true or false keywords directly in your document. MongoDB natively supports boolean data types, making it simple to store yes/no, on/off, or enabled/disabled states. Syntax db.collection.insertOne({ "fieldName": true, "anotherField": false }); Sample Data Let us create a collection with employee documents containing boolean fields − db.demo215.insertMany([ {"EmployeeDetails": [{"EmployeeName": "David", "isMarried": false, "Salary": 56000}]}, {"EmployeeDetails": [{"EmployeeName": "Bob", "isMarried": true, "Salary": 60000}]}, {"EmployeeDetails": [{"EmployeeName": "Chris", ...
Read MoreAggregate multiple arrays into one huge array with MongoDB?
To aggregate multiple arrays into a single array in MongoDB, use the $concatArrays operator within the $project stage of an aggregation pipeline. This combines arrays from different fields into one unified array. Syntax db.collection.aggregate([ { $project: { combinedArray: { $concatArrays: ["$array1", "$array2", "$array3"] } ...
Read MoreUsing addFields pipeline and run with the MongoDB filter operator
The $addFields pipeline stage combined with the $filter operator allows you to add new fields or modify existing ones by filtering array elements based on specific conditions. Syntax db.collection.aggregate([ { $addFields: { "fieldName": { $filter: { input: ...
Read MoreMongoDB Aggregate sum of values in a list of dictionaries for all documents?
To aggregate sum of values in a list of dictionaries for all documents, use $unwind to flatten the array, then $group with $sum operators to calculate totals by grouping field. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField.groupByField", "fieldSum1": { $sum: "$arrayField.field1" }, ...
Read MoreHow to find only a single document satisfying the criteria in MongoDB?
To find only a single document satisfying specific criteria in MongoDB, use the findOne() method. This method returns the first matching document from the collection, even if multiple documents satisfy the query criteria. Syntax db.collection.findOne(query, projection) Sample Data db.demo116.insertMany([ {"EmployeeId": 101, "EmployeeName": "John"}, {"EmployeeId": 102, "EmployeeName": "Bob"}, {"EmployeeId": 103, "EmployeeName": "David"}, {"EmployeeId": 102, "EmployeeName": "Mike"} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreFind maximum score for duplicate Name values in MongoDB?
To find the maximum score for duplicate Name values in MongoDB, use the aggregation pipeline with $group and $max operators. This groups documents by name and calculates the highest score for each group. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", maxScore: { $max: "$scoreField" } } } ]); ...
Read More