Articles on Trending Technologies

Technical articles with clear explanations and examples

MongoDB transaction & indexes for duplicate values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 344 Views

To prevent duplicate values within array fields in MongoDB, use createIndex() with the unique option. When a unique index is applied to an array field, MongoDB treats each array element as a separate index entry, preventing any value from appearing in multiple documents. Syntax db.collection.createIndex( { "arrayField": 1 }, { "unique": true } ); Sample Data Let us create a collection with documents ? db.demo298.insertMany([ { Name: "Chris", Marks: [46, 79] }, { Name: "David", Marks: ...

Read More

MongoDB query to insert but limit the total records

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 297 Views

To insert documents while limiting the total records in a MongoDB collection, use capped collections with the capped: true option and set the max parameter to define the maximum number of documents. Syntax db.createCollection("collectionName", { capped: true, size: sizeInBytes, max: maxDocuments }); Create Capped Collection Let us create a capped collection that allows a maximum of 4 documents ? db.createCollection("demo297", {capped: true, size: 4, max: 4}); { "ok" : 1 } Insert Documents Now ...

Read More

Native Querying MongoDB inside array and get the count

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 173 Views

To query inside array and check for existence to get the count, use $exists. This operator checks whether a specific field exists within array elements and returns documents that match the condition. Syntax db.collection.count({ "arrayField.nestedField": { $exists: true } }); Sample Data db.demo296.insertMany([ { "id": 101, "Name": "Chris", "details": [ ...

Read More

Querying object's field array values in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 207 Views

To query object's field array values in MongoDB, use the field name directly with the array element value. MongoDB automatically searches within array fields to find documents containing the specified value. Syntax db.collection.find({arrayFieldName: "value"}); Sample Data db.demo295.insertMany([ {"status": ["Active", "Inactive"]}, {"status": ["Yes", "No"]} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e4d4ea65d93261e4bc9ea39"), ObjectId("5e4d4eb15d93261e4bc9ea3a") ] } ...

Read More

MongoDB: combining AND and OR?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 700 Views

In MongoDB, you can combine AND and OR operators to create complex query conditions. Use $and to ensure all conditions are met, and $or to match any one of multiple conditions within the same query. Syntax db.collection.find({ $and: [ { $or: [ { "field1": "value1" }, ...

Read More

MongoDB - How to check for equality in collection and in embedded document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 167 Views

To check for equality between a field in the main collection and a field in an embedded document, use the $where operator with JavaScript expressions. This allows you to compare values across different nesting levels within the same document. Syntax db.collection.find({ $where: 'this.fieldName === this.embeddedDoc.fieldName' }); Sample Data db.demo292.insertMany([ { "FirstName": "Chris", "LastName": "Brown", "Friend": { ...

Read More

MongoDB - How to get the sum of two columns and save it to another column?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 637 Views

To get the sum of two columns and save it to another column in MongoDB, use the $add operator within an aggregation pipeline with the $project stage. Syntax db.collection.aggregate([ { $project: { field1: "$field1", field2: "$field2", sumField: { $add: ["$field1", "$field2"] } ...

Read More

MongoDB query for exact match

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 894 Views

For exact match queries in MongoDB, you can combine the $exists operator with field conditions to ensure precise matching, especially when dealing with fields that could contain either single values or arrays. Syntax db.collection.find({ $and: [ {"field.0": {$exists: false}}, {"field": "exactValue"} ] }); Sample Data db.demo290.insertMany([ {"ListOfName": "Chris"}, {"ListOfName": ["Chris", "David"]} ]); { "acknowledged": ...

Read More

Make an array of multiple arrays in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 305 Views

To make an array of multiple arrays in MongoDB, use $unwind in the aggregation pipeline to deconstruct array fields. This operation creates separate documents for each array element, effectively flattening multiple arrays into individual elements. Syntax db.collection.aggregate([ { $unwind: "$arrayField" } ]); Sample Data db.demo289.insertMany([ {"Section": ["A", "B", "E"], "Name": "Chris"}, {"Section": ["C", "D", "B"], "Name": "David"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Implement MongoDB $push in embedded document array?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 320 Views

To implement MongoDB $push in an embedded document array, use the $push operator to add new documents to an existing array field within a document. Syntax db.collection.update( { "field": "value" }, { $push: { "arrayField": { "newDocument": "value" } } } ); Sample Data Let us create a collection with documents − db.demo288.insertOne({ "Name": "Chris", "details": [ {"CountryName": "US", "Marks": 78}, ...

Read More
Showing 23211–23220 of 61,297 articles
Advertisements