AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 548 of 840

Check for duplicates in an array in MongoDB?

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

To check for duplicates in an array in MongoDB, use the $unwind and $group operators within an aggregation pipeline to identify array elements that appear more than once in the same document. Syntax db.collection.aggregate([ { $project: { arrayField: 1 } }, { $unwind: "$arrayField" }, { $group: { _id: { _id: "$_id", value: "$arrayField" }, count: { $sum: 1 } } }, { $match: { count: { $gt: 1 } } }, { $group: { _id: "$_id._id", ...

Read More

Sort MongoDB field which contains both integer and decimal values?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 448 Views

To sort a MongoDB field containing both integer and decimal values, use the sort() method. MongoDB automatically handles mixed numeric types (integers and decimals) in the same field and sorts them by their numeric value. Syntax db.collection.find().sort({fieldName: 1}); // 1 for ascending, -1 for descending Sample Data db.demo755.insertMany([ {"Value": 10}, {"Value": 10.5}, {"Value": 9.5}, {"Value": 12.5}, {"Value": 11.5}, {"Value": 6} ]); { ...

Read More

How to convert birth date records to age with MongoDB

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

To convert birth date records to age in MongoDB, use the $dateDiff operator or calculate the difference between the current date and birth date using $subtract and $divide operators in an aggregation pipeline. Syntax db.collection.aggregate([ { $project: { age: { $divide: [ ...

Read More

Filter specific values from a MongoDB document

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 639 Views

To filter specific values from a MongoDB document, use the $filter operator in aggregation pipelines. This operator allows you to select array elements that match specific conditions and return only those elements. Syntax { $filter: { input: "$arrayField", as: "variable", cond: { condition } } } Sample Data db.demo751.insertOne({ _id: 101, details: [ ...

Read More

How to get max values for distinct elements in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 775 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 533 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 187 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
Showing 5471–5480 of 8,392 articles
« Prev 1 546 547 548 549 550 840 Next »
Advertisements