Big Data Analytics Articles

Page 61 of 135

MongoDB query to count records on the basis of matching criteria

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 682 Views

To count records on the basis of matching criteria in MongoDB, use the countDocuments() method with a query filter. This method returns the number of documents that match the specified criteria. Syntax db.collection.countDocuments({field1: value1, field2: value2}) Sample Data Let us create a collection with documents ? db.demo205.insertMany([ { "id": "101", "Name": "", "Age": "", "isActive": false ...

Read More

Display only an element found in an array in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 347 Views

To display only an element found in an array in MongoDB, use aggregate() with $match, $unwind, and $project stages to filter and extract specific array elements. Syntax db.collection.aggregate([ { "$match": { "arrayField.searchField": "searchValue" }}, { "$unwind": "$arrayField" }, { "$match": { "arrayField.searchField": "searchValue" }}, { "$project": { "fieldToDisplay": "$arrayField.fieldName", "_id": 0 }} ]); Sample Data db.demo204.insertOne({ "_id": 101, "Name": "Chris", "Age": 23, ...

Read More

Query nested array by more than one condition in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 786 Views

To query nested array by more than one condition in MongoDB, use the $elemMatch operator. This operator matches documents that contain an array field with at least one element that satisfies all the specified query conditions. Syntax db.collection.find({ "arrayField": { $elemMatch: { "field1": "value1", "field2": "value2" } } ...

Read More

Display MongoDB explain query plan?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 413 Views

To analyze MongoDB query performance and execution strategy, use the explain() method. This provides detailed information about how MongoDB processes your queries, including execution stages, index usage, and performance metrics. Syntax db.collection.find(query).explain(); db.collection.find(query).explain("executionStats"); db.collection.find(query).explain("allPlansExecution"); Sample Data db.demo202.insertMany([ {"StudentFirstName": "Chris", "StudentAge": 21}, {"StudentFirstName": "David", "StudentAge": 23}, {"StudentFirstName": "Bob", "StudentAge": 22} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e3c3bd103d395bdc21346e8"), ...

Read More

Update only a specific value in a MongoDB document

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 306 Views

To update only a specific value in a MongoDB document, use the $set operator with update() or updateOne(). This operator modifies only the specified fields without affecting other document data. Syntax db.collection.updateOne( { "field": "matchValue" }, { $set: { "fieldToUpdate": "newValue" } } ); Create Sample Data db.demo201.insertMany([ { "ClientName": "Chris Brown", "ClientAge": 26 }, { "ClientName": "David Miller", "ClientAge": 35 }, { "ClientName": "Carol Taylor", "ClientAge": 28 } ]); ...

Read More

MongoDB query to pull / unset with multiple conditions?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 412 Views

To remove array elements that meet multiple conditions in MongoDB, use the $pull operator with query conditions. The $pull operator removes all array elements that match the specified criteria. Syntax db.collection.update( {matchCriteria}, { $pull: { "arrayField": {condition} } }, { multi: true } ); Sample Data db.demo198.insertMany([ {"List": {"Values": [10, 20, 30, 30, 70, 80, 90]}}, {"List": {"Values": [56, 978, 56, 34, 23, 34]}}, {"List": {"Values": [21, 12, 14, ...

Read More

In MongoDB will limit() increase query speed?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 172 Views

No, using limit() does not increase query execution speed, but it does reduce bandwidth consumption and memory usage by returning fewer documents. The database still processes the same number of documents but transfers less data to the client. Syntax db.collection.find().limit(number) Sample Data db.demo197.insertMany([ {"Name": "Chris"}, {"Name": "Bob"}, {"Name": "David"}, {"Name": "Sam"}, {"Name": "Mike"}, {"Name": "Carol"}, {"Name": "John"} ]); { ...

Read More

MongoDB query in object of arrays

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 239 Views

To query arrays within nested objects in MongoDB, use dot notation to navigate through the object hierarchy and target specific array fields. MongoDB will match documents where the array contains the specified value. Syntax db.collection.find({ "object.nestedObject.arrayField": "value" }); Sample Data db.demo194.insertMany([ { "_id": 101, "details": { "otherDetails": { ...

Read More

How to improve the execution time of a query in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 300 Views

To improve the execution time of a query in MongoDB, use indexes on frequently queried fields. Indexes create efficient data structures that allow MongoDB to quickly locate documents without scanning the entire collection. Syntax db.collection.createIndex({ "fieldName": 1 }, { unique: true }); db.collection.find({ "fieldName": "value" }); Create Sample Data with Index First, create a unique index on the LastName field for faster queries ? db.demo193.createIndex({"LastName": 1}, {unique: true}); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, ...

Read More

How to get values of cursor in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 738 Views

To get values of cursor in MongoDB, use the hasNext() method to iterate through cursor results. A cursor is returned by query operations like find() and allows you to process documents one by one. Syntax var cursor = db.collection.find(query); while (cursor.hasNext()) { print(tojson(cursor.next())); } Sample Data db.demo191.insertMany([ {"EmployeeId": 1, "EmployeeName": "Chris Brown"}, {"EmployeeId": 2, "EmployeeName": "David Miller"}, {"EmployeeId": 1, "EmployeeName": "John Doe"}, {"EmployeeId": 1, "EmployeeName": "John Smith"} ]); { ...

Read More
Showing 601–610 of 1,348 articles
« Prev 1 59 60 61 62 63 135 Next »
Advertisements