Database Articles

Page 56 of 547

A single MongoDB query to orderby date and group by user

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 320 Views

To order by date and group by user in a single MongoDB query, use the aggregate() method with $group and $sort stages. This approach groups documents by user and finds the maximum date for each user, then sorts the results. Syntax db.collection.aggregate([ { $group: { _id: "$userId", dueDate: { $max: "$dueDate" } } ...

Read More

Easiest way to sort an array in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 665 Views

The easiest way to sort an array in MongoDB is to use the aggregation pipeline with $unwind, $sort, and $group operators. This approach unwraps the array, sorts the elements, and reconstructs the document. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $sort: { "arrayField.sortField": 1 } }, { $group: { _id: "$_id", arrayField: { $push: "$arrayField" } } } ]); Sample Data db.demo242.insertOne({ "details2": [ { "ShipingDate": new ISODate("2019-10-11"), "Price": ...

Read More

Get the maximum mark records from a collection with documents in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 199 Views

To get the maximum mark records from a collection in MongoDB, use the $sort and $limit aggregation operators to sort documents by marks in descending order and retrieve only the top records. Syntax db.collection.aggregate([ { $sort: { marks: -1 } }, { $limit: numberOfRecords } ]); Sample Data Let us create a collection with student documents containing marks − db.students.insertMany([ { "StudentName": "Chris", "marks": 85 }, { "StudentName": "Bob", "marks": 92 }, ...

Read More

MongoDB query for documents whose array elements does not have a specific value

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 587 Views

To query documents whose array elements do not have a specific value in MongoDB, use the $elemMatch operator combined with $exists: false. This finds documents containing arrays where at least one element lacks the specified field. Syntax db.collection.find({ "arrayField": { "$elemMatch": { "fieldToCheck": { "$exists": false } } } }); Sample Data Let us create a collection with ...

Read More

Implement MongoDB Aggregate - unwind, group and project?

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

The MongoDB aggregation framework allows you to process data through a pipeline of stages. Three powerful operators - $unwind, $group, and $project - can be combined to deconstruct arrays, group documents, and shape the output. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$groupField", result: { $operator: "$field" } } }, { $project: { field1: 1, field2: 1, _id: 0 } } ]); Sample Data db.demo238.insertMany([ { ...

Read More

Remove all except a single field from a nested document via projection in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 423 Views

To remove all except a single field from a nested document in MongoDB, use projection with the find() method. Set unwanted fields to 0 to exclude them, keeping only the desired field visible. Syntax db.collection.find( {}, { "nestedDocument.unwantedField1": 0, "nestedDocument.unwantedField2": 0 } ); Sample Data db.demo237.insertOne({ _id: 101, Product: { ...

Read More

How to sort, select and query subdocument in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 335 Views

To sort, select and query subdocument in MongoDB, use the aggregation pipeline with $unwind, $project, and $sort stages. This approach extracts subdocument fields, selects specific fields, and sorts the results. Syntax db.collection.aggregate([ { $unwind: "$subdocumentField" }, { $project: { field1: "$subdocumentField.field1", field2: "$subdocumentField.field2" }}, { $sort: { field1: 1 } } ]); Sample Data db.demo236.insertMany([ {"details":{"Name":"Chris", ...

Read More

Is there any way in MongoDB to get the inner value of json data?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 249 Views

To get inner values of JSON data in MongoDB, use the find() method along with dot notation to access nested fields within documents and arrays. Syntax db.collection.find( {}, { "outerField.innerField": 1, "_id": 0 } ); Sample Data db.demo235.insertOne({ "id": 101, "details": [ { "Name": "Chris Brown", ...

Read More

Using MongoDB Aggregate and GroupBy to get the frequency of name record

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 213 Views

To get the frequency of name records in MongoDB, use the aggregation framework with $group stage to group documents by name and count occurrences using $sum. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", Frequency: { $sum: 1 } } } ]); Create Sample Data db.demo232.insertMany([ ...

Read More

Change a unique index to a sparse unique index in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 275 Views

To change a unique index to a sparse unique index in MongoDB, you must drop the existing index and recreate it with the sparse: true option. MongoDB doesn't support modifying index properties directly. Syntax // Drop existing unique index db.collection.dropIndex("indexName"); // Create sparse unique index db.collection.createIndex({"field": 1}, {unique: true, sparse: true}); Example: Converting Unique to Sparse Unique Index Step 1: Create Initial Unique Index db.demo229.createIndex({"ClientName": 1}, {unique: true}); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, ...

Read More
Showing 551–560 of 5,468 articles
« Prev 1 54 55 56 57 58 547 Next »
Advertisements