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 14 of 111
Group with multiple fields and get the count of duplicate field values grouped together innMongoDB
To group documents with multiple fields and count duplicate combinations in MongoDB, use the aggregation framework with $project and $group stages. The $cond operator helps normalize field values for consistent grouping regardless of field order. Syntax db.collection.aggregate([ { $project: { field1: { $cond: { if: condition, then: value1, else: value2 } }, field2: { $cond: { if: condition, then: value1, else: ...
Read MoreHow do I delete array value from a document in MongoDB?
To delete array values from a document in MongoDB, use the $pull operator. The $pull operator removes from an existing array all instances of a value or values that match a specified condition. Syntax db.collection.update( { "matchCondition": "value" }, { $pull: { "arrayField": "valueToRemove" } } ); Create Sample Data Let us first create a collection with documents − db.demo535.insertOne({ "studentId": "101", "studentName": "Chris", "ListOfMailIds": [ ...
Read MoreUnable to implement $addToSet in MongoDB to fetch values of a single field?
The $addToSet operator adds values to an array unless the value is already present, in which case $addToSet does nothing to that array. When used with $group in aggregation, it creates a unique set of values from a field across multiple documents. Syntax db.collection.aggregate([ { $group: { _id: groupingField, arrayField: { $addToSet: "$fieldName" } ...
Read MoreMongoDB query to fetch only the "Name" field based on roles?
To fetch only the "Name" field based on specific roles in MongoDB, use the aggregation framework with $group, $setIsSubset, and $match operators. This approach filters documents by comparing their roles against a target role set. Syntax db.collection.aggregate([ { $group: { _id: "$Name", "types": { $addToSet: "$Type" } } }, { $project: { _id: 1, "types": 1, "isSubset": { $setIsSubset: [["TargetRole"], "$types"] } } }, { $match: { "isSubset": false } }, { $group: { _id: "$isSubset", "Name": { $push: "$_id" } ...
Read MoreHow I can use a database-name with special characters like " customer_tracker-990" in MongoDB console
To use a database name with special characters like "customer_tracker-990" in MongoDB console, use the getSiblingDB() method. This method allows you to reference databases with names that contain special characters like hyphens, spaces, or other symbols. Syntax db.getSiblingDB("database-name-with-special-chars").collection.operation() Sample Data Let us add some documents to the database with special characters − use customer_tracker-990; db.demo1.insertMany([ {"Name": "Chris"}, {"Name": "David"}, {"Name": "Bob"} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreGet the top most document from a MongoDB collection
To get the topmost document from a MongoDB collection, use the find() method along with limit(1). This retrieves the first document based on the natural insertion order or specified sorting criteria. Syntax db.collection.find().limit(1); Sample Data db.demo681.insertMany([ {_id: 101, Name: "Chris"}, {_id: 102, Name: "Bob"}, {_id: 103, Name: "David"}, {_id: 104, Name: "Bob"}, {_id: 105, Name: "Sam"} ]); { "acknowledged": true, "insertedIds": [101, 102, 103, 104, 105] ...
Read MoreSet gt condition in MongoDB and
The $and operator performs a logical AND operation on an array of one or more expressions. You can combine the $gt condition with other criteria using $and to create complex queries that match documents satisfying multiple conditions. Syntax db.collection.find({ $and: [ { field: { $gt: value } }, { field: anotherCondition } ] }); Sample Data db.demo680.insertMany([ { Values: 40 }, ...
Read MorePull an element in sub of sub-array in MongoDB?
To pull an element from a sub-array within a nested array in MongoDB, use $pull combined with the $ positional operator. This allows you to target and remove specific elements from deeply nested array structures. Syntax db.collection.update( { "parentArray.field": "matchValue" }, { $pull: { "parentArray.$.subArray": { "field": "valueToRemove" } } } ); Create Sample Data db.demo679.insertOne({ id: 1, "details": [ { ...
Read MoreHow to index my collection to use a compound multikey index?
To create a compound multikey index on your MongoDB collection, use the createIndex() method (modern replacement for the deprecated ensureIndex()). A compound multikey index combines multiple fields where at least one field contains array values. Syntax db.collection.createIndex({ "field1": 1, "arrayField.nestedField": 1 }); Create Sample Data First, let's create the compound multikey index on our collection ? db.demo678.createIndex({id: 1, "details.userId": 1}); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, ...
Read MoreMongoDB query to add up the values of a specific field in documents
To add up the values of a specific field across all documents in MongoDB, use the $sum aggregation operator within a $group stage. This calculates the total sum of numeric field values. Syntax db.collection.aggregate([ { $group: { _id: null, sum: { $sum: "$fieldName" } } } ]); Create Sample Data Let us create a collection with sample documents ? db.demo677.insertMany([ { Value: 10 }, { Value: 50 }, { Value: 20 }, ...
Read More