Articles on Trending Technologies

Technical articles with clear explanations and examples

MongoDB aggregation group and remove duplicate array values?

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

To group documents and remove duplicate values from arrays in MongoDB, use the aggregation framework with $unwind to deconstruct arrays and $addToSet within $group to collect unique values. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$groupByField", arrayField: { $addToSet: "$arrayField" }, otherField: { $first: "$otherField" } }} ]); Sample Data db.demo649.insertMany([ ...

Read More

MongoDB aggregation to combine or merge fields and then count?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 690 Views

To combine or merge fields and then perform count, use $group along with $sum and $sort. This aggregation approach groups documents by field values and counts occurrences in each group. Syntax db.collection.aggregate([ { $group: { "_id": "$fieldName", "COUNT": { $sum: 1 } } }, { $sort: { "COUNT": -1 } }, { $limit: n } ]); Sample Data db.demo647.insertMany([ {"Subject": "MySQL"}, {"Subject": "MongoDB"}, {"Subject": "MySQL"}, ...

Read More

Move different elements to another array in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 374 Views

To move different elements to another array in MongoDB, use forEach with filter() methods to separate elements based on criteria and save() to update the document. This approach allows you to move specific elements from one array to another within the same document. Syntax db.collection.find({}).forEach(function(doc) { doc.targetArray = doc.sourceArray.filter(function(element) { return condition; }); doc.sourceArray = doc.sourceArray.filter(function(element) { return !condition; }); ...

Read More

MongoDB query to store File Name and location?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 608 Views

To store file name and location in MongoDB, create documents with fileName and fileLocation fields. This approach is useful for file management systems, content libraries, or any application that needs to track file metadata. Syntax db.collection.insertOne({ "fileName": "filename.extension", "fileLocation": "/path/to/file/location" }); Sample Data Let us create a collection with file documents using insertMany() ? db.demo645.insertMany([ { "fileName": "MongoDB Program", "fileLocation": "C:/users/workspace/AllMongoDBProgram/MongoDB Program" ...

Read More

How to update a Timestamp and set to current date in MongoDB?

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

To update a timestamp and set it to the current date in MongoDB, use the update() method with $set operator and JavaScript's new Date() to get the current timestamp. Syntax var currentDate = new Date(); db.collection.update( {}, { $set: { "timestampField": currentDate } }, { multi: true } ); Sample Data Let us first create a collection with documents ? db.demo644.insertMany([ { "ShippingDate": new ISODate("2018-04-19") }, { "ShippingDate": new ISODate("2019-01-10") ...

Read More

Updating Nested Embedded Documents in MongoDB?

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

To update nested embedded documents in MongoDB, use the positional operator ($) with update operations like $push, $set, or $addToSet. The positional operator identifies the array element that matches the query condition and allows you to modify nested documents within that element. Syntax db.collection.update( {"arrayField.nestedField": "matchValue"}, {"$push": {"arrayField.$.nestedArray": newDocument}} ); Sample Data Let's create a collection with nested embedded documents ? db.demo643.insertOne({ details: [ { ...

Read More

MongoDB query to get specific list of names from documents where the value of a field is an array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 318 Views

To get specific list of names from documents where the value of a field is an array, use the $all operator. The $all operator selects documents where the array field contains all the specified elements. Syntax db.collection.find({ arrayField: { $all: [ "element1", "element2", ... ] } }); Sample Data Let us create a collection with documents containing arrays of names ? db.demo642.insertMany([ { _id: ...

Read More

Get specific elements from embedded array in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 444 Views

To get specific elements from an embedded array in MongoDB, use the $unwind operator to deconstruct the array, then $match with dot notation to filter elements based on specific criteria. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.fieldName": value } }, { $project: { _id: 0, arrayField: 1 } } ]); Sample Data db.demo641.insertOne({ ProductId: 101, "ProductInformation": [ { ...

Read More

Filtering MongoDB items by fields and subfields?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 541 Views

To filter MongoDB documents by fields and subfields, use dot notation to access nested properties. This allows you to query specific values within embedded documents or check for field existence. Syntax db.collection.find({"field.subfield": value}); db.collection.find({"field.subfield": null}); db.collection.find({"field.subfield": {$exists: true}}); Sample Data db.demo638.insertMany([ {Name: "Chris"}, {Name: "David", details: {Subject: "MongoDB"}}, {Name: "John", details: {Subject: "JavaScript", Level: "Advanced"}} ]); WriteResult({ "nInserted" : 3 }) Display all documents from the collection ? db.demo638.find().pretty(); { ...

Read More

MongoDB - interpret information on the query plan for the db.collection.find() method

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 183 Views

To interpret information on the query plan for the db.collection.find() method in MongoDB, use the explain() method. This provides detailed insights into query execution including index usage and performance characteristics. Syntax db.collection.find(query).explain() db.collection.find(query).explain("executionStats") Sample Data Create a collection with an index and insert sample documents: db.demo637.createIndex({ClientName: 1}); db.demo637.insertMany([ {ClientName: "John"}, {ClientName: "Bob"}, {ClientName: "Johnson"} ]); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, ...

Read More
Showing 22961–22970 of 61,297 articles
Advertisements