Big Data Analytics Articles

Page 4 of 135

How to count a cursor's iteration in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 240 Views

To count a cursor's iteration in MongoDB, you need to use custom logic with a while loop along with a find() cursor. This allows you to iterate through documents and count specific conditions during traversal. Syntax var cursor = db.collection.find(); var count = 0; while (cursor.hasNext()) { var document = cursor.next(); // Apply your counting logic here if (condition) { count++; } } Sample Data db.demo724.insertMany([ ...

Read More

Get count of array elements from a specific field in MongoDB documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 850 Views

To count array elements from a specific field in MongoDB, use the $size operator within an aggregation pipeline. This operator returns the number of elements in an array field for each document. Syntax db.collection.aggregate([ { $project: { count: { $size: "$arrayFieldName" } } } ]); Sample Data db.demo723.insertMany([ {"Subject": ["MySQL", "MongoDB"]}, {"Subject": ["C"]}, {"Subject": ["C++", "Java", "Python"]} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Count by multiple fields with MongoDB aggregation

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 886 Views

To count by multiple fields in MongoDB, use the $facet operator combined with $group and $sum. The $facet processes multiple aggregation pipelines within a single stage on the same set of input documents. Syntax db.collection.aggregate([ { $facet: { "pipeline1": [ /* aggregation stages */ ], "pipeline2": [ /* aggregation stages */ ] } ...

Read More

Update all the values of a field with a specific string in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 314 Views

To update all the values of a field with a specific string in MongoDB, use the update() method with an empty filter and the multi: true option to affect all documents in the collection. Syntax db.collection.update( {}, { $set: { "fieldName": "newValue" } }, { multi: true } ); Sample Data db.demo720.insertMany([ { "SubjectName": "MySQL" }, { "SubjectName": "Java" }, { "SubjectName": "C" }, { ...

Read More

MongoDB query to add a new field and concatenate the price result divided by a specific number in it

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 482 Views

To add a new field and concatenate price results divided by a specific number in MongoDB, use the $addFields stage with $reduce and $map operators to process array values and combine them into a single string. Syntax db.collection.aggregate([ { $addFields: { newField: { $reduce: { ...

Read More

How can I sort documents in MongoDB 4 and display only a single field?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 197 Views

To sort documents in MongoDB and display only a single field, use the sort() method combined with projection. The sort() method orders documents, while projection controls which fields appear in the result. Syntax db.collection.find({}, {fieldName: 1}).sort({fieldName: 1}); Where 1 means ascending order and -1 means descending order. Sample Data db.demo611.insertMany([ {"Name": "Chris"}, {"Name": "Adam"}, {"Name": "John"}, {"Name": "Bob"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

How to add together a subset of elements of an array in MongoDB aggregation?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 272 Views

To add together a subset of elements of an array in MongoDB aggregation, use the $slice operator combined with $sum to select and sum specific array elements based on position or range. Syntax db.collection.aggregate([ { $project: { subsetSum: { $sum: { ...

Read More

MongoDB query to remove entire array from collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 515 Views

To remove an entire array field from all documents in a MongoDB collection, use the $unset operator. This completely removes the specified field from the documents. Syntax db.collection.updateMany( {}, { $unset: { "arrayFieldName": "" } } ); Sample Data Let us create a collection with documents containing array fields ? db.demo609.insertMany([ { "ListOfSubject": ["MySQL", "MongoDB"] }, { "ListOfSubject": ["Java"] } ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Getting unique values within two arrays in one MongoDB document

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 2K+ Views

To get unique values within two arrays in a MongoDB document, use the $setUnion operator in the aggregation pipeline. The $setUnion takes two or more arrays and returns an array containing the unique elements that appear in any input array. Syntax db.collection.aggregate([ { $project: { uniqueArray: { $setUnion: ["$array1", "$array2"] } } } ]) Sample Data Let us ...

Read More

MongoDB query to update the nested document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 509 Views

To update a nested document in MongoDB, use the update() method with dot notation to specify the path to the nested field. This allows you to modify deeply embedded values without affecting the parent document structure. Syntax db.collection.update( { "filterField": "value" }, { $set: { "parentField.nestedField.targetField": "newValue" } } ); Sample Data Let us create a collection with a nested document structure ? db.demo607.insertOne({ id: 1, "Info1": { ...

Read More
Showing 31–40 of 1,348 articles
« Prev 1 2 3 4 5 6 135 Next »
Advertisements