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 17 of 547
MongoDB 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 MoreMongoDB Group query to get the count of repeated marks in documents?
To get the count of repeated marks in MongoDB documents, use the $group aggregation stage with $sum to count occurrences of each unique mark value. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", count: { $sum: 1 } } } ]) Sample Data db.demo676.insertMany([ ...
Read MoreWhat does the max field mean in the output of MongoDB db..stats( )?
The max field in MongoDB's db..stats() output represents the maximum number of documents allowed in a capped collection. This field only appears for capped collections and shows the document limit set during collection creation. Syntax db.createCollection("collectionName", { capped: true, size: sizeInBytes, max: maxDocuments }); Create Sample Capped Collection Let's create a capped collection with a maximum of 50 documents ? db.createCollection("demo673", { capped: true, size: 100, ...
Read MoreGet fields from multiple sub-documents that match a condition in MongoDB?
To get fields from multiple sub-documents that match a condition in MongoDB, use the aggregation pipeline with $unwind to flatten nested arrays, $match to filter documents, and $project to select specific fields. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.nestedField": "condition" } }, { $project: { fieldName: "$arrayField.targetField" } } ]); Sample Data db.demo671.insertOne({ "details": [ { ...
Read More