MongoDB Articles

Page 56 of 111

Creating views in MongoDB

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

To create views in MongoDB, use the createView() method. Views are read-only virtual collections that display data from underlying collections based on aggregation pipelines. Syntax db.createView( "viewName", "sourceCollection", [aggregationPipeline] ) Sample Data Let us create a collection with sample documents ? db.demo113.insertMany([ { _id: 1, StudentId: "101", "Details": { Name: "Chris", Age: 21 }, Subject: "MySQL" }, { _id: 2, StudentId: "102", "Details": { Name: "Alice", Age: 20 }, Subject: "MongoDB" }, ...

Read More

How to update a single field in a capped collection in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 347 Views

To update a single field in a capped collection in MongoDB, use the updateOne() or updateMany() method with the $set operator. Capped collections allow field updates but do not permit operations that would increase document size. Syntax db.collection.updateOne( { "field": "matchValue" }, { $set: { "field": "newValue" } } ); Create Sample Capped Collection First, create a capped collection with size and document limits ? db.createCollection("Demo112", { capped : true, size : 14, max : 3 } ); { "ok" : ...

Read More

MongoDB Aggregation to slice array inside array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 492 Views

To slice an array inside another array in MongoDB, use the aggregate() method with $addFields, $map, and $slice operators. This allows you to limit the number of elements returned from nested arrays. Syntax db.collection.aggregate([ { $addFields: { "arrayField": { $map: { "input": "$arrayField", ...

Read More

How to only get the data of the nested JSON object in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 978 Views

To get the data of the nested JSON object in MongoDB, use findOne() with projection and the $elemMatch operator to filter and return only specific nested array elements. Syntax db.collection.findOne( { "matchField": "value" }, { "arrayField": { $elemMatch: { "nestedField": "value" } } } ); Sample Data db.demo109.insertOne({ "Name": "Chris", "Subjects": [ { "Id": "100", ...

Read More

How to work with variables in MongoDB query

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

To use variables in MongoDB queries, work with the var keyword to declare variables and reference them in your queries. This approach makes queries more flexible and reusable. Syntax var variableName = "value"; db.collection.find({"field": variableName}); Sample Data Let us create a collection with documents ? db.demo107.insertMany([ {"Name": "Chris"}, {"Name": "Bob"}, {"Name": "David"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e2ee1b19fd5fd66da214471"), ...

Read More

Insert array where element does not exist else update it (with multiple conditions)?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 343 Views

To insert array elements when they don't exist or update them when they do using multiple conditions, use bulkWrite() with multiple updateOne operations. This allows conditional updates and insertions in a single atomic operation. Syntax db.collection.bulkWrite([ { "updateOne": { "filter": { "field": "value", "array": { "$elemMatch": { conditions } } }, "update": { "$set": { "array.$.field": "newValue" } } ...

Read More

Implement $dateToString on array items with MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 308 Views

To implement $dateToString on array items in MongoDB, use the $map operator inside an aggregation pipeline to transform date fields within array elements into formatted strings. Syntax db.collection.aggregate([ { $project: { "arrayField": { $map: { "input": "$arrayField", ...

Read More

MongoDB Aggregate JSON array field for the matching field of other collection?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 642 Views

To aggregate JSON array field for matching fields from another collection in MongoDB, use $lookup combined with $unwind to join collections based on array elements, then reconstruct the result using $group. Syntax db.collection1.aggregate([ { $unwind: "$arrayField" }, { $lookup: { from: "collection2", localField: "arrayField.joinField", foreignField: "_id", as: "matchedData" }}, ...

Read More

MongoDB query to implement nor query to fetch documents except a specific document

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 208 Views

To fetch documents except a specific document, use the $nor operator in MongoDB. The $nor operator performs a logical NOR operation, returning documents that do not match any of the conditions in the array. Syntax db.collection.find({ $nor: [ { field1: value1 }, { field2: value2 } ] }); Sample Data Let's create a collection with sample documents ? db.demo100.insertMany([ { "Name": "Chris", "Age": 21 ...

Read More

How to group nested fields in MongoDB aggregation with count value in array?

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

To group nested fields in MongoDB aggregation with count values from arrays, use the $objectToArray operator to convert nested objects into key-value pairs, then use $unwind and $group to aggregate the count values. Syntax db.collection.aggregate([ { $project: { fieldName: { $objectToArray: "$nestedObject" } } }, { $unwind: "$fieldName" }, { $group: { _id: "$fieldName.k", count: { $sum: "$fieldName.v.countField" } } } ]) Sample Data db.demo99.insertMany([ { "Details": { ...

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