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
Big Data Analytics Articles
Page 8 of 135
MongoDB query for counting the distinct values across all documents?
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 MoreAggregation: group date in nested documents (nested object) and display the count?
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 MoreCreating hierarchical JSON in MongoDB?
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 MoreRemoving an object in a child collection in MongoDB?
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 MoreMongoDB compound conditions to fetch documents?
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 MoreUpdate tag records in MongoDB quickly
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 MoreSort id and reverse the items with MongoDB
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 MoreMongoDB aggregation to get two documents with the least marks
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 MoreFind MongoDB records based on a condition?
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 MoreSet server status to inactive in a MongoDB collection with server records?
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