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
Database Articles
Page 64 of 547
Get the count of a specific value in MongoDB
To get the count of a specific value in MongoDB, use the aggregate() method with $unwind and $group stages to count occurrences of array elements or use $size with $filter for a more efficient approach. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.field": "targetValue" } }, { $group: { _id: "$_id", count: { $sum: 1 } } } ]); Sample Data db.demo210.insertOne({ details: [ { ClientName: "Robert" ...
Read MoreMongoDB query to convert the field value and create datetime day of month during projection?
To convert a field value and create datetime day of month during projection in MongoDB, use aggregate() with $unwind to flatten arrays and $dayOfMonth to extract the day from converted timestamps. Syntax db.collection.aggregate([ { "$unwind": "$arrayField" }, { "$project": { "fieldName": 1, "DayOfMonth": { ...
Read MoreSpecify a return format for data in MongoDB
In MongoDB, you can specify a custom return format for your data using aggregation operators like $addToSet. This operator collects unique values from multiple documents and returns them as an array, eliminating duplicates. Syntax db.collection.aggregate([ { "$group": { "_id": null, "fieldName": { "$addToSet": "$sourceField" } } }, ...
Read MoreMongoDB query to determine if a specific value does not exist?
To determine if a specific value does not exist, use the $ne (not equal) operator in MongoDB. This operator matches documents where the field value does not equal the specified value. Syntax db.collection.find({ "field": { "$ne": "value" } }) Sample Data db.demo206.insertMany([ { "ClientDetails": [ { "Name": "Chris", ...
Read MoreMongoDB query to count records on the basis of matching criteria
To count records on the basis of matching criteria in MongoDB, use the countDocuments() method with a query filter. This method returns the number of documents that match the specified criteria. Syntax db.collection.countDocuments({field1: value1, field2: value2}) Sample Data Let us create a collection with documents ? db.demo205.insertMany([ { "id": "101", "Name": "", "Age": "", "isActive": false ...
Read MoreDisplay only an element found in an array in MongoDB?
To display only an element found in an array in MongoDB, use aggregate() with $match, $unwind, and $project stages to filter and extract specific array elements. Syntax db.collection.aggregate([ { "$match": { "arrayField.searchField": "searchValue" }}, { "$unwind": "$arrayField" }, { "$match": { "arrayField.searchField": "searchValue" }}, { "$project": { "fieldToDisplay": "$arrayField.fieldName", "_id": 0 }} ]); Sample Data db.demo204.insertOne({ "_id": 101, "Name": "Chris", "Age": 23, ...
Read MoreQuery nested array by more than one condition in MongoDB
To query nested array by more than one condition in MongoDB, use the $elemMatch operator. This operator matches documents that contain an array field with at least one element that satisfies all the specified query conditions. Syntax db.collection.find({ "arrayField": { $elemMatch: { "field1": "value1", "field2": "value2" } } ...
Read MoreDisplay MongoDB explain query plan?
To analyze MongoDB query performance and execution strategy, use the explain() method. This provides detailed information about how MongoDB processes your queries, including execution stages, index usage, and performance metrics. Syntax db.collection.find(query).explain(); db.collection.find(query).explain("executionStats"); db.collection.find(query).explain("allPlansExecution"); Sample Data db.demo202.insertMany([ {"StudentFirstName": "Chris", "StudentAge": 21}, {"StudentFirstName": "David", "StudentAge": 23}, {"StudentFirstName": "Bob", "StudentAge": 22} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e3c3bd103d395bdc21346e8"), ...
Read MoreUpdate only a specific value in a MongoDB document
To update only a specific value in a MongoDB document, use the $set operator with update() or updateOne(). This operator modifies only the specified fields without affecting other document data. Syntax db.collection.updateOne( { "field": "matchValue" }, { $set: { "fieldToUpdate": "newValue" } } ); Create Sample Data db.demo201.insertMany([ { "ClientName": "Chris Brown", "ClientAge": 26 }, { "ClientName": "David Miller", "ClientAge": 35 }, { "ClientName": "Carol Taylor", "ClientAge": 28 } ]); ...
Read MoreMongoDB query to pull / unset with multiple conditions?
To remove array elements that meet multiple conditions in MongoDB, use the $pull operator with query conditions. The $pull operator removes all array elements that match the specified criteria. Syntax db.collection.update( {matchCriteria}, { $pull: { "arrayField": {condition} } }, { multi: true } ); Sample Data db.demo198.insertMany([ {"List": {"Values": [10, 20, 30, 30, 70, 80, 90]}}, {"List": {"Values": [56, 978, 56, 34, 23, 34]}}, {"List": {"Values": [21, 12, 14, ...
Read More