Big Data Analytics Articles

Page 8 of 135

MongoDB query for counting the distinct values across all documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 519 Views

To count distinct values across all documents in MongoDB, use the aggregate() method with $unwind, $addToSet, and $size operators. This approach flattens arrays, groups unique values, and counts them. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: null, uniqueValues: { $addToSet: "$arrayField" } } }, { $project: { _id: 0, count: { $size: "$uniqueValues" } } } ]); Sample Data db.demo718.insertMany([ { "id": 101, ...

Read More

Aggregation: group date in nested documents (nested object) and display the count?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 313 Views

To group dates in nested documents and display their count in MongoDB, use the aggregation pipeline with $unwind to flatten the array, then $group to count occurrences of each date. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField.dateField", count: { $sum: 1 } } ...

Read More

Creating hierarchical JSON in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 271 Views

To create hierarchical JSON in MongoDB, structure your documents using nested objects and arrays. MongoDB natively supports complex JSON structures with multiple levels of nesting, allowing you to model relationships within a single document. Syntax db.collection.insertOne({ "field1": "value1", "field2": "value2", "nestedObject": { "subField1": "value", "subField2": "value", "nestedArray": [ { ...

Read More

Removing an object in a child collection in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 674 Views

To remove an object in a child collection in MongoDB, use the $pull operator combined with the $ positional operator to target the specific nested array and remove matching elements. Syntax db.collection.update( {"parentArray.field": "matchValue"}, { $pull: { "parentArray.$.childArray": {field: "valueToRemove"} } } ); Sample Data db.demo715.insertOne({ _id: 1, details: [ { 'id': '101', ...

Read More

MongoDB compound conditions to fetch documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 170 Views

MongoDB compound conditions allow you to fetch documents by combining multiple criteria using logical operators. This enables complex filtering with AND, OR, and other conditional operators. Syntax // AND condition (implicit) db.collection.find({field1: "value1", field2: "value2"}) // OR condition db.collection.find({$or: [{field1: "value1"}, {field2: "value2"}]}) // IN operator for multiple values db.collection.find({field: {$in: ["value1", "value2"]}}) Sample Data db.demo714.insertMany([ {FirstName: "Chris", LastName: "Brown"}, {FirstName: "Adam", LastName: "Smith"}, {FirstName: "David", LastName: "Miller"}, {FirstName: "John", LastName: "Doe"} ]); ...

Read More

Update tag records in MongoDB quickly

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 155 Views

To update tag records in MongoDB quickly, use the $ positional operator along with the update command. The $ operator identifies the first array element that matches the query condition and updates it efficiently. Syntax db.collection.update( { "arrayField.field": "matchValue" }, { $set: { "arrayField.$.field": "newValue" } } ); Create Sample Data Let us create a collection with tag documents ? db.demo713.insertOne({ tags: [ { ...

Read More

Sort id and reverse the items with MongoDB

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

The $natural operator returns documents in natural insertion order. To reverse the items and get them in reverse insertion order, use $natural:-1 with the sort() method. Syntax db.collection.find().sort({$natural: -1}); Sample Data Let us create a collection with documents ? db.demo710.insertMany([ {id: 101, Name: "Robert"}, {id: 102, Name: "Carol"}, {id: 103, Name: "Mike"}, {id: 104, Name: "Sam"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

MongoDB aggregation to get two documents with the least marks

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 205 Views

To get the two documents with the least marks in MongoDB, use the aggregation pipeline with $sort to order by marks in ascending order and $limit: 2 to restrict the result to only two documents. Syntax db.collection.aggregate([ { $sort: { "field": 1 } }, { $limit: 2 } ]); Sample Data db.demo709.insertMany([ { "Name": "John", "Marks": 75 }, { "Name": "Chris", "Marks": 45 }, { "Name": "David", "Marks": 54 }, ...

Read More

Find MongoDB records based on a condition?

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

To find MongoDB records based on a condition, use the find() method with a query document that specifies the filtering criteria. The query document contains field-value pairs that define the conditions to match. Syntax db.collection.find({ field: value }); db.collection.find({ field: { $operator: value } }); Sample Data Let us create a collection with student documents ? db.students.insertMany([ { "Name": "John", "Marks": 54 }, { "Name": "Chris", "Marks": 35 }, { "Name": "David", "Marks": 45 }, { ...

Read More

Set server status to inactive in a MongoDB collection with server records?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 406 Views

To set a server status to inactive in a MongoDB collection, use the positional operator $ with the $set update operator to target specific array elements based on matching criteria. Syntax db.collection.update( { "arrayField.matchField": "matchValue" }, { $set: { "arrayField.$.updateField": "newValue" } } ); Sample Data Let us create a collection with server records − db.demo707.insertOne({ id: 101, "serverInformation": [ { ...

Read More
Showing 71–80 of 1,348 articles
« Prev 1 6 7 8 9 10 135 Next »
Advertisements