Database Articles

Page 37 of 547

MongoDB $addToSet to add a deep nested array of object?

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

The $addToSet operator adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array. When working with deep nested arrays, combine $addToSet with the positional operator $ to target specific array elements. Syntax db.collection.update( { "parentArray.field": "matchValue" }, { $addToSet: { "parentArray.$.nestedArray": newObject } } ); Sample Data Let us first create a collection with documents − db.demo380.insertOne({ "details": [ { ...

Read More

MongoDB projection on specific nested properties?

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

To project specific nested properties in MongoDB, use the aggregation pipeline with $addFields, $objectToArray, $filter, and $arrayToObject operators to selectively include nested fields based on conditions. Syntax db.collection.aggregate([ { "$addFields": { "nested.path": { "$arrayToObject": { "$filter": { ...

Read More

Manipulating subdocuments in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 210 Views

To manipulate subdocuments in MongoDB, use dot notation to target specific fields within embedded documents or arrays. The $ positional operator helps update elements that match query conditions. Syntax db.collection.update( { "arrayField.field": "matchValue" }, { $operation: { "arrayField.$.targetField": value } } ); Create Sample Data db.demo378.insertOne({ Name: "Chris", details: [ { id: 101, Score: 56 }, { id: 102, Score: 78 ...

Read More

How can I concatenate an array of integer in MongoDB aggregation method?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 505 Views

To concatenate an array of integers in MongoDB aggregation, use the $reduce operator with $concat to combine array elements into a single string. Since integers must be converted to strings for concatenation, use $toString for type conversion. Syntax db.collection.aggregate([ { $project: { concatenatedField: { $reduce: { ...

Read More

How to update after aggregate in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 719 Views

To update documents after performing aggregation analysis in MongoDB, you cannot directly update within the aggregation pipeline. Instead, use the update() method with array filters to target specific array elements based on conditions. Syntax db.collection.update( { "field": value }, { $inc: { "arrayField.$[identifier].field": incrementValue } }, { arrayFilters: [{ "identifier.field": condition }] } ); Sample Data Let us first create a collection with documents ? db.demo376.insertOne({ "id": 101, "details": [ ...

Read More

MongoDB query to execute stored function?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 619 Views

JavaScript functions can be saved for reuse using a system collection called system.js. To store a function, use db.collection.save(), then execute it with db.eval(). Syntax // Store function db.system.js.save({ _id: "functionName", value: function(parameters) { // function body return result; } }); // Execute function db.eval("functionName(arguments)"); Example: Store and Execute Function Let us first create a function ? db.system.js.save({ _id: ...

Read More

MongoDB difference between show dbs and show databases?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 372 Views

In MongoDB, there is no functional difference between show dbs and show databases. Both commands are aliases that internally call the same listDatabases command and produce identical output. Syntax show dbs OR show databases Example: Using show dbs show dbs admin 0.002GB app 0.000GB business 0.000GB config 0.000GB ...

Read More

Find when the keys are unknown in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 747 Views

To find documents when the keys are unknown in MongoDB, use $addFields with $objectToArray to convert documents into key-value arrays, then search through the values dynamically. Syntax db.collection.aggregate([ { "$addFields": { "arrayField": { "$objectToArray": "$$ROOT" } }}, { "$match": { "arrayField.v.fieldName": "searchValue" }}, { "$project": { "arrayField": 0 }} ]) Sample Data db.demo375.insertMany([ { "details": { ...

Read More

Find values group by another field in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 531 Views

To group by another field in MongoDB, use $group along with $project. This approach allows you to aggregate documents based on a specific field while collecting values from other fields. Syntax db.collection.aggregate([ {$match: {field: "value"}}, {$group: {"_id": "$groupByField", "collectedField": {$addToSet: "$fieldToCollect"}}}, {$project: {"_id": 0, "groupByField": "$_id", "collectedField": 1}} ]); Sample Data db.demo374.insertMany([ { "Name": "Chris", "HobbyDetails": ["Reading Book", "Playing Football"], ...

Read More

How to index and sort with pagination using custom field in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 461 Views

To implement index and sorting with pagination using custom fields in MongoDB, combine compound indexes with sort(), limit(), and skip() methods. This approach ensures efficient query performance for paginated results. Syntax db.collection.createIndex({"field1": 1, "field2": 1}); db.collection.find().sort({"field1": 1, "field2": 1}).limit(pageSize).skip(pageNumber * pageSize); Create Sample Data First, create a compound index on Name and CountryName fields for optimal sorting performance ? db.demo373.createIndex({"Name": 1, "CountryName": 1}); { "createdCollectionAutomatically": true, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1 } ...

Read More
Showing 361–370 of 5,468 articles
« Prev 1 35 36 37 38 39 547 Next »
Advertisements