Database Articles

Page 5 of 547

How to get max values for distinct elements in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 773 Views

To get max values for distinct elements in MongoDB, use the $group operator with $max to find the highest value for each distinct element. This aggregation pipeline groups documents by a field and returns the maximum value within each group. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", maxValue: { $max: "$valueField" } } ...

Read More

MongoDB query to update each field of documents in collection with a formula?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 287 Views

To update each field of documents in collection with a formula in MongoDB, use the $mul operator with the positional all operator $[] to apply mathematical operations across all array elements. Syntax db.collection.update( {}, { $mul: { "arrayField.$[].fieldName": formulaValue } }, { multi: true } ); Sample Data db.demo749.insertOne({ "details": [ {"id": 1, "a": 10}, {"id": 2, "a": 5}, ...

Read More

What is the maximum size of a document in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 532 Views

The maximum size of a document in MongoDB is 16 MB (16, 777, 216 bytes). This limit ensures efficient memory usage and prevents documents from consuming excessive resources during operations. Syntax // Document structure with size limit { field1: "value1", field2: "value2", // ... additional fields // Total document size ≤ 16 MB } Sample Data Let us create a collection with sample documents ? db.demo748.insertMany([ {_id: 101, Name: "Chris", Age: 21}, ...

Read More

Pushing values into array with multi field set to TRUE?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 185 Views

To push values into arrays across multiple documents in MongoDB, use the $push operator with update() and set the multi field to true. This allows updating all matching documents in a single operation. Syntax db.collection.update( {filter}, { $push: { arrayField: "value" } }, { multi: true } ); Sample Data db.demo747.insertMany([ { "CountryName": ["US", "IND"] }, { "CountryName": ["UK", "US"] }, { "CountryName": ["UK", "IND"] } ]); ...

Read More

Find posts that are older than current date in MongoDB?

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

To find posts older than current date in MongoDB, use the $lte operator with new Date() to compare document dates against the current system date. Syntax db.collection.find({ dateField: { $lte: new Date() } }); Sample Data Let us create a collection with documents containing different due dates ? db.demo746.insertMany([ { DueDate: new Date("2020-01-10") }, { DueDate: new Date("2020-10-10") }, { DueDate: new Date("2020-03-05") }, { DueDate: new Date("2020-05-04") } ]); ...

Read More

Concatenate with condition in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 499 Views

To concatenate with condition in MongoDB, use $cond with $concat in an aggregation pipeline. This allows you to perform string concatenation based on specific conditions and filter results accordingly. Syntax db.collection.aggregate([ { "$redact": { "$cond": [ { "$eq": [{ "$concat": ["$field1", "$field2"] }, "targetValue"] }, ...

Read More

How to find a certain element in the MongoDB embedded document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 237 Views

To find a certain element in a MongoDB embedded document, use the aggregation pipeline with $unwind to flatten the array, $match to filter specific elements, and $project to select desired fields. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { "arrayField.field": "value" } }, { $project: { "arrayField.desiredField": 1 } } ]) Sample Data db.demo744.insertOne({ studentInformation: [ { ...

Read More

How can I extract entire documents based on how they compare with their whole collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 181 Views

To extract entire documents based on how they compare with their whole collection in MongoDB, use the $$ROOT variable within an aggregation pipeline. This allows you to preserve complete documents while performing grouping and comparison operations. Syntax db.collection.aggregate([ { $project: { "field1": "$field1", "field2": "$field2", "document": "$$ROOT" ...

Read More

I can print out each element of an array by iterating through all values, but can't get a specific element in MongoDB

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

In MongoDB, you can fetch specific elements from arrays using projection operators like $elemMatch or array indexing, which is more efficient than iterating through all values with forEach(). Syntax // Using array index db.collection.find({}, {"arrayField.index": 1}); // Using $elemMatch db.collection.find({}, {arrayField: {$elemMatch: {field: "value"}}}); Sample Data db.demo742.insertOne({ "userDetails": [ { "userName": "Robert", "CountryName": "UK" ...

Read More

Updating MongoDB collection for _id?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 200 Views

To update a document in MongoDB using its _id field, use the $set operator within an update operation. The _id field serves as the unique identifier for each document in a collection. Syntax db.collection.update( { _id: ObjectId("document_id") }, { $set: { field: "newValue" } } ); Sample Data Let us create a collection with sample documents ‒ db.demo741.insertMany([ { SubjectName: "MySQL" }, { SubjectName: "C" }, { SubjectName: "Java" } ]); ...

Read More
Showing 41–50 of 5,468 articles
« Prev 1 3 4 5 6 7 547 Next »
Advertisements