MongoDB Articles

Page 19 of 111

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 762 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 463 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 215 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 480 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 513 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

How to find if element exists in document - MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 630 Views

To find if an element exists in a MongoDB document, use the $exists operator. This operator checks whether a specified field is present in the document, regardless of its value. Syntax db.collection.find({"fieldName": {$exists: true}}); db.collection.find({"fieldName": {$exists: false}}); Sample Data db.demo497.insertMany([ {"details": [{"Name": "Chris"}, {"Name": "Bob"}]}, {"details": [{"Name": "Carol"}]}, {"details": [{}]} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e84b3cfb0f3fa88e22790d1"), ...

Read More

Display the undefined and exact MongoDB document records

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 358 Views

To display undefined and exact MongoDB document records, use the forEach() method with printjson(). The forEach() callback accepts two parameters: the document and the index, allowing you to display either the actual documents or undefined values. Syntax db.collection.find({}).forEach((document, index) => { printjson(document); // Display documents printjson(index); // Display undefined }); Sample Data db.demo496.insertMany([ { "Name": "David", "CountryName": "US" }, { "Name": "John", "CountryName": "AUS" }, { "Name": "Robert", ...

Read More

Update only a single MongoDB document without deleting any date

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 280 Views

To update only a single document in MongoDB, use the updateOne() method. This method updates the first document that matches the specified filter criteria and preserves all existing data except for the fields being updated. Syntax db.collection.updateOne( { "field": "matchValue" }, { $set: { "fieldToUpdate": "newValue" } } ); Create Sample Data Let us create a collection with documents − db.demo495.insertMany([ {"FirstName":"Chris", "Age":19}, {"FirstName":"David", "Age":21}, {"FirstName":"Bob", "Age":26}, {"FirstName":"John", ...

Read More

Update elements inside an array in MongoDB?

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

To update elements inside an array in MongoDB, use the $set operator combined with the $ positional operator. The positional operator identifies the array element that matches the query condition and allows you to update specific fields within that element. Syntax db.collection.update( { "arrayField.field": "matchValue" }, { $set: { "arrayField.$.fieldToUpdate": "newValue" } } ); Create Sample Data db.demo494.insertOne({ "CollegeDetails": [ { ...

Read More
Showing 181–190 of 1,106 articles
« Prev 1 17 18 19 20 21 111 Next »
Advertisements