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
Fetch specific field values in MongoDB
To fetch specific field values in MongoDB, use the $in operator to match documents where a field value equals any value in a specified array, combined with projection to return only the desired fields. Syntax db.collection.find( { fieldName: { $in: ["value1", "value2", "value3"] } }, { field1: 1, field2: 0, _id: 0 } ); Sample Data Let us create a collection with student documents ? db.indexesDemo.insertMany([ {"StudentFirstName": "John", "StudentLastName": "Smith"}, {"StudentFirstName": "Chris", "StudentLastName": "Brown"}, ...
Read MoreAppending an entry in one to many embedded documents with MongoDB
To append an entry in MongoDB one-to-many embedded documents, use the $push operator which adds new elements to an existing array field without affecting other array elements. Syntax db.collection.update( { "matchField": "value" }, { $push: { "arrayField": newDocument } } ); Create Sample Data Let us create a collection with documents ? db.demo253.insertOne({ _id: "101", isActive: false, details: [ { ...
Read MoreSort array in MongoDB query and project all fields?
To sort an array within a document and project all fields in MongoDB, use the aggregation pipeline with $unwind, $sort, $group, and $project stages. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $sort: { "arrayField.field": 1 } }, { $group: { _id: "$_id", arrayField: { $push: "$arrayField" } } }, { $project: { _id: 1, arrayField: 1 } } ]); Sample Data db.demo252.insertOne({ "Values": [ { ...
Read MorePerform min/max with MongoDB aggregation
To find minimum and maximum values in MongoDB, use the $min and $max operators within the aggregation pipeline's $group stage. These operators analyze numeric fields across documents and return the smallest and largest values respectively. Syntax db.collection.aggregate([ { $group: { _id: null, maxValue: { $max: "$fieldName" }, minValue: { ...
Read MoreFind the MongoDB collection size for name "Chris
To find the MongoDB collection size for documents with specific field values, use Object.bsonsize() within a forEach() loop to calculate the total BSON size of matching documents. Syntax var totalSize = 0; db.collection.find({fieldName: "value"}).forEach(function(doc) { totalSize += Object.bsonsize(doc); }); print(totalSize); Sample Data db.demo250.insertMany([ {"Name": "Chris"}, {"Name": "Bob"}, {"Name": "David"}, {"Name": "Chris"} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreQuery BinData by Type in MongoDB
To query BinData documents by their subtype in MongoDB, use the subtype() method within a JavaScript function to filter documents based on their binary data type. Syntax db.collection.find(function(){ return this.fieldName.subtype() == subtypeNumber }); Sample Data Let us create a collection with documents containing different BinData subtypes − db.demo249.insertMany([ { "_id": BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") }, { "_id": BinData(4, "CNDF66qIlCY92q1vFAAAAQ==") }, { "_id": BinData(3, "CNDF66qJ29g92q1vFAAAEw==") } ]); { "acknowledged": true, ...
Read MoreMongoDB aggregate to convert multiple documents into single document with an array?
MongoDB aggregation pipeline allows you to group multiple documents into single documents containing arrays. Use the $group stage with $push operator to combine documents sharing a common field into a single document with an array field. Syntax db.collection.aggregate([ { $group: { _id: "$groupingField", arrayField: { ...
Read MoreMongoDB query to skip n first documents?
To skip a specific number of documents in MongoDB, use the skip() method along with limit(). The skip() method bypasses the first n documents and returns the remaining documents from the result set. Syntax db.collection.find().skip(n).limit(count); Sample Data Let us create a collection with documents ? db.demo246.insertMany([ {"StudentFirstName": "Chris", "StudentLastName": "Brown"}, {"StudentFirstName": "John", "StudentLastName": "Doe"}, {"StudentFirstName": "John", "StudentLastName": "Smith"}, {"StudentFirstName": "Carol", "StudentLastName": "Taylor"} ]); { "acknowledged" : true, ...
Read MoreCoalesce values from different properties into a single array with MongoDB aggregation
To coalesce values from different properties into a single array in MongoDB, use the $group stage to collect values with $addToSet, then $project with $setUnion to merge them into one unified array. Syntax db.collection.aggregate([ { $group: { "_id": null, "property1": { $addToSet: "$field1" }, "property2": { $addToSet: "$field2" } ...
Read MoreA single MongoDB query to orderby date and group by user
To order by date and group by user in a single MongoDB query, use the aggregate() method with $group and $sort stages. This approach groups documents by user and finds the maximum date for each user, then sorts the results. Syntax db.collection.aggregate([ { $group: { _id: "$userId", dueDate: { $max: "$dueDate" } } ...
Read More