Articles on Trending Technologies

Technical articles with clear explanations and examples

How to insert a boolean field in MongoDB?

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

To insert a boolean field in MongoDB, use the true or false keywords directly in your document. MongoDB natively supports boolean data types, making it simple to store yes/no, on/off, or enabled/disabled states. Syntax db.collection.insertOne({ "fieldName": true, "anotherField": false }); Sample Data Let us create a collection with employee documents containing boolean fields − db.demo215.insertMany([ {"EmployeeDetails": [{"EmployeeName": "David", "isMarried": false, "Salary": 56000}]}, {"EmployeeDetails": [{"EmployeeName": "Bob", "isMarried": true, "Salary": 60000}]}, {"EmployeeDetails": [{"EmployeeName": "Chris", ...

Read More

Aggregate multiple arrays into one huge array with MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 418 Views

To aggregate multiple arrays into a single array in MongoDB, use the $concatArrays operator within the $project stage of an aggregation pipeline. This combines arrays from different fields into one unified array. Syntax db.collection.aggregate([ { $project: { combinedArray: { $concatArrays: ["$array1", "$array2", "$array3"] } ...

Read More

Using addFields pipeline and run with the MongoDB filter operator

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

The $addFields pipeline stage combined with the $filter operator allows you to add new fields or modify existing ones by filtering array elements based on specific conditions. Syntax db.collection.aggregate([ { $addFields: { "fieldName": { $filter: { input: ...

Read More

MongoDB Aggregate sum of values in a list of dictionaries for all documents?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 436 Views

To aggregate sum of values in a list of dictionaries for all documents, use $unwind to flatten the array, then $group with $sum operators to calculate totals by grouping field. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField.groupByField", "fieldSum1": { $sum: "$arrayField.field1" }, ...

Read More

How to find only a single document satisfying the criteria in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 157 Views

To find only a single document satisfying specific criteria in MongoDB, use the findOne() method. This method returns the first matching document from the collection, even if multiple documents satisfy the query criteria. Syntax db.collection.findOne(query, projection) Sample Data db.demo116.insertMany([ {"EmployeeId": 101, "EmployeeName": "John"}, {"EmployeeId": 102, "EmployeeName": "Bob"}, {"EmployeeId": 103, "EmployeeName": "David"}, {"EmployeeId": 102, "EmployeeName": "Mike"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Find maximum score for duplicate Name values in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 209 Views

To find the maximum score for duplicate Name values in MongoDB, use the aggregation pipeline with $group and $max operators. This groups documents by name and calculates the highest score for each group. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", maxScore: { $max: "$scoreField" } } } ]); ...

Read More

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 350 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 498 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 985 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
Showing 23311–23320 of 61,297 articles
Advertisements