MongoDB Articles

Page 18 of 111

Update the last row with search criteria in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 425 Views

To update the last row that matches specific search criteria in MongoDB, use the findAndModify() method with a sort parameter to identify the most recent document. Syntax db.collection.findAndModify({ query: { field: "value" }, sort: { _id: -1 }, update: { $set: { field: "newValue" } }, new: true }); Sample Data db.demo516.insertMany([ { "Name": "John", "Age": 22, "Score": 56 }, { "Name": "John", "Age": 23, "Score": 67 }, ...

Read More

MongoDB: Find name similar to Regular Expression input?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 142 Views

To find names similar to a regular expression input in MongoDB, use the $regex operator with the $options parameter for case-insensitive matching. Syntax db.collection.find({ "fieldName": { $regex: "pattern", $options: "i" } }); Sample Data db.demo514.insertMany([ {"Information": {"FullName": "John Doe"}}, {"Information": {"FullName": "John Smith"}}, {"Information": {"FullName": "john doe"}}, {"Information": {"FullName": "Chris Brown"}} ]); ...

Read More

Sum the score of duplicate column values in MongoDB documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 237 Views

To sum values of duplicate column entries in MongoDB, use the $group stage with $sum aggregation operator. This groups documents by a field and calculates the total of specified numeric fields. Syntax db.collection.aggregate([ { $group: { _id: "$groupingField", field1Sum: { $sum: "$field1" }, field2Sum: { $sum: "$field2" } ...

Read More

MongoDB query to find matching documents given an array with values?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 246 Views

To find matching documents given an array with values in MongoDB, use the $in operator. This operator matches documents where the array field contains any of the specified values. Syntax db.collection.find({ "arrayField": { $in: ["value1", "value2", "value3"] } }); Create Sample Data Let us create a collection with documents containing project arrays ? db.demo511.insertMany([ { "ListOfProject": ["Library Management System", "Hospital Management System"] }, { ...

Read More

How to match and group array elements with the max value in MongoDB aggregation?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 673 Views

To match and group array elements with the maximum value in MongoDB aggregation, use $filter with $max to find elements with the highest score, then $group to count occurrences by name. Syntax db.collection.aggregate([ { "$project": { "maxElement": { "$arrayElemAt": [ ...

Read More

Implement MongoDB $addToSet for an array in an array and append a value

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 428 Views

To implement MongoDB $addToSet for an array within an array and append a value, use the update() method with dot notation to target the specific nested array. The $addToSet operator adds a value to an array only if it doesn't already exist, preventing duplicates. Syntax db.collection.update( {query}, { $addToSet: { "parentArray.index.nestedArray": value } } ); Sample Data db.demo509.insertOne({ "value1": [ { ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 184 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 374 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 320 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
Showing 171–180 of 1,106 articles
« Prev 1 16 17 18 19 20 111 Next »
Advertisements