Big Data Analytics Articles

Page 18 of 135

Does MongoDB track how many times each index is used in a query?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 195 Views

Yes, MongoDB tracks index usage statistics through the $indexStats aggregation stage. This allows you to monitor how frequently each index is accessed, helping optimize database performance by identifying unused or heavily used indexes. Syntax db.collection.aggregate([ { $indexStats: {} } ]); Create Sample Data First, let's create an index on the FirstName field − db.demo508.createIndex({"FirstName": 1}); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1 } Now insert ...

Read More

How to filter some fields in objects and fetch a specific subject name value in MongoDB?

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

To filter and fetch specific fields from objects in MongoDB, use the aggregation pipeline combining $match, $project, and $filter operators to target specific array elements and extract only the desired fields. Syntax db.collection.aggregate([ { $match: { "arrayField.field": "value" } }, { $project: { arrayField: { $filter: { input: "$arrayField", ...

Read More

Can't find user by name with MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 380 Views

To find a user by name in MongoDB, use the find() method with a query document that specifies the name field and its value. This performs an exact match search on the specified field. Syntax db.collection.find({"fieldName": "value"}); Create Sample Data Let us create a collection with user documents − db.demo504.insertMany([ {"Name": "Chris"}, {"Name": "John"}, {"Name": "David"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

MongoDB slice array in populated field?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 323 Views

To slice arrays in MongoDB, use the $slice operator in the projection part of your query. This allows you to return only a portion of an array field from documents. Syntax db.collection.find( { query }, { "arrayField": { $slice: count } } ); Where count can be: Positive number: Returns first N elements Negative number: Returns last N elements [skip, limit]: Skips elements and returns limited count Sample Data db.demo503.insertMany([ {_id:1, Name:"John", Subject:["MySQL", "Java", "C"]}, ...

Read More

MongoDB query to find on field combination of FirstName and LastName?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 769 Views

To query MongoDB for a field combination of FirstName and LastName, use $concat to combine the fields into a single string and $eq to check equality against your target value. Syntax db.collection.aggregate([ { "$redact": { "$cond": [ { "$eq": [ ...

Read More

MongoDB query to group duplicate documents

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 469 Views

To group duplicate documents in MongoDB, use the $group stage in the aggregation pipeline. This operation groups documents by a specified field and eliminates duplicates by creating a single document per unique value. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName" } } ]); Sample Data db.demo501.insertMany([ { "Name": "Chris" }, { "Name": "Bob" }, { "Name": "Chris" }, { "Name": "John" }, { "Name": "Chris" }, ...

Read More

MongoDB query to select 10 most recent documents without changing order?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 217 Views

To select the 10 most recent documents in MongoDB without changing their order, use the skip() method with count() - 10. This skips the oldest documents and returns the last 10 in their original insertion order. Syntax db.collection.find().skip(db.collection.count() - 10); Create Sample Data db.demo500.insertMany([ {value: 10}, {value: 1200}, {value: 19}, {value: 28}, {value: 50}, {value: 70}, {value: 100}, {value: 10}, ...

Read More

Query array of subdocuments in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 483 Views

To query an array of subdocuments in MongoDB, you can use various operators like $unwind in aggregation pipelines, $elemMatch for matching specific array elements, or simple dot notation for basic queries. Syntax // Using $unwind in aggregation db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.field": "value" } } ]); // Using $elemMatch db.collection.find({ "arrayField": { $elemMatch: { "field": "value" } } }); Sample Data db.demo499.insertOne({ "details": [ ...

Read More

Retrieving group by result with arrays in MongoDB?

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

To retrieve group by result with arrays in MongoDB, use aggregate() with $unwind and $group stages. The $addToSet operator adds values to an array unless the value is already present, preventing duplicates. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$fieldToGroupBy", resultArray: { $addToSet: "$fieldToCollect" } } ...

Read More

Change date format in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 522 Views

To change date format in MongoDB, you can manipulate date strings using JavaScript variables and string methods within the MongoDB shell, or use MongoDB's date aggregation operators for more complex transformations. Syntax // JavaScript string manipulation var inputDate = "01-10-2019"; var formatDate = inputDate.split(/-|\//); var outputString = formatDate[2] + '-' + formatDate[0] + '-' + formatDate[1]; // MongoDB aggregation for date formatting db.collection.aggregate([ { $project: { formattedDate: { $dateToString: { format: "%Y-%m-%d", date: "$dateField" } } }} ]); ...

Read More
Showing 171–180 of 1,348 articles
« Prev 1 16 17 18 19 20 135 Next »
Advertisements