Articles on Trending Technologies

Technical articles with clear explanations and examples

Using findOneAndUpdate () to update in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 385 Views

The findOneAndUpdate() method updates a single document based on the filter criteria and returns the document (either the original or modified version based on options). Syntax db.collection.findOneAndUpdate(filter, update, options) filter − Query criteria to match the document update − Update operations to perform options − Optional parameters like returnNewDocument, sort, etc. Sample Data Let us create a collection with sample documents − db.demo328.insertMany([ {Name: "Chris", Marks: 67}, {Name: "David", Marks: 78}, {Name: "Bob", Marks: 97} ]); ...

Read More

MongoDB query to set user defined variable into query?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 518 Views

To set user-defined variables in MongoDB queries, use the var keyword to declare variables and reference them directly in your query conditions. Syntax var variableName = "value"; db.collection.find({"fieldName": variableName}); Sample Data db.demo327.insertMany([ {"FirstName": "Chris", "LastName": "Brown"}, {"FirstName": "David", "LastName": "Miller"}, {"FirstName": "John", "LastName": "Doe"}, {"FirstName": "John", "LastName": "Smith"} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e516952f8647eb59e562076"), ...

Read More

Accessing array values in a MongoDB collection to fetch a specific document

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 293 Views

To access array values in MongoDB and fetch a specific document, use dot notation to query nested fields within arrays. This allows you to search for documents containing specific values in their array elements. Syntax db.collection.find({ "arrayName.fieldName": "value" }); Sample Data Let us create a collection with documents containing ProductDetails arrays ? db.demo326.insertMany([ { "id": 101, "ProductDetails": [ { "ProductId": ...

Read More

MongoDB many insertsupdates without affecting performance?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 237 Views

To perform many inserts and updates in MongoDB without affecting performance, use bulk operations like insertMany() for inserts and proper indexing to optimize query performance. Create indexes on frequently queried fields to speed up both read and write operations. Syntax // Bulk insert db.collection.insertMany([document1, document2, ...]); // Create index for better performance db.collection.createIndex({fieldName: 1}); Sample Data Let us create a collection with multiple documents using insertMany() ? db.demo325.insertMany([ { _id: 101, Name: "Chris", Age: 23 }, { _id: 102, Name: "David", Age: 24 ...

Read More

Using positional operator with hierarchies in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 158 Views

The positional operator in MongoDB allows you to update array elements without knowing their exact index position. The $[] operator updates all array elements, while $ updates the first matching element. Syntax // Update all array elements db.collection.update( { query }, { $set: { "arrayField.$[]": newValue } } ); // Update first matching element db.collection.update( { "arrayField": matchValue }, { $set: { "arrayField.$": newValue } } ); Sample Data db.demo324.insertOne({ "ListOfValues": [10, ...

Read More

Array index or indexing inner items in MongoDB to fetch values

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 170 Views

MongoDB uses dot notation to access nested fields and array elements. You can create indexes on nested fields to improve query performance when searching within embedded documents or arrays. Syntax // Create index on nested field db.collection.createIndex({"field.nestedField": 1}); // Query nested field db.collection.find({"field.nestedField": "value"}); Sample Data Setup Let's create a collection with nested documents and add an index on the nested field ? db.demo323.insertMany([ {"details": {"Name": "Chris", "Age": 34}}, {"details": {"Name": "David", "Age": 31}}, {"details": {"Name": "Bob", "Age": 28}} ...

Read More

Check for existing documents/embedded documents in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 300 Views

To check for existing documents or embedded documents in MongoDB, use the $exists operator. This operator matches documents where a specified field exists, regardless of its value. Syntax db.collection.find({ "fieldName": { $exists: true } }) db.collection.find({ "embeddedField.subField": { $exists: true } }) Sample Data Let us create a collection with documents containing embedded arrays ? db.demo322.insertMany([ { "id": 1001, "details": [ ...

Read More

How to store array values in MongoDB?

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

MongoDB supports array data types natively, allowing you to store multiple values within a single document field. Arrays can contain simple values, objects, or even nested arrays. Syntax db.collection.insertOne({ "fieldName": [value1, value2, value3] }); // For arrays of objects db.collection.insertOne({ "fieldName": [ {"key1": "value1"}, {"key2": "value2"} ] }); Sample Data Let us create a collection with documents containing array values ? db.demo321.insertMany([ ...

Read More

MongoDB query for capped sub-collection in an array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 474 Views

In MongoDB, you cannot create capped sub-collections within arrays. However, you can limit the number of array elements returned using the $slice operator in aggregation pipelines to achieve similar functionality. Syntax db.collection.aggregate([ { $project: { fieldName: { $slice: [ "$arrayField", numberOfElements ] } } } ]); Sample Data db.demo319.insertMany([ {"Scores": [100, 345, 980, 890]}, {"Scores": [903, 10004, 84575, 844]} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

MongoDB query to pull a specific value from a document

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 550 Views

To pull a specific value from a document in MongoDB, use the $pull operator with the update() method. The $pull operator removes all instances of a specified value from an existing array. Syntax db.collection.update( { query }, { $pull: { "arrayField": "valueToRemove" } } ) Sample Data Let us create a collection with documents ? db.demo318.insertMany([ { Subject: ["MySQL", "MongoDB", "Java"] }, { Subject: ["Spring", "Hibernate"] } ]) { ...

Read More
Showing 23161–23170 of 61,297 articles
Advertisements