MongoDB Articles

Page 34 of 111

MongoDB query to filter only the logs containing the "work" word in the content

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 209 Views

To filter logs containing the word "work" in MongoDB, use the $filter operator with $indexOfBytes to perform case-insensitive substring matching within an aggregation pipeline. Syntax db.collection.aggregate([ { "$addFields": { "arrayField": { "$filter": { "input": "$arrayField", ...

Read More

MongoDB aggregate $slice to get the length of the array

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 480 Views

To get the length of an array in MongoDB using aggregation, use the $size operator within a $project stage. The $size operator returns the number of elements in the specified array field. Syntax db.collection.aggregate([ { $project: { "fieldName": { $size: "$arrayField" } } } ]); Sample Data Let us create a collection with documents ? db.demo382.insertOne({ "Name": "David", "details": [ { ...

Read More

How to search an array for values present in another array and output the indexes of values found into a new array in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 663 Views

To search an array for values present in another array and output the indexes of values found into a new array in MongoDB, use the $indexOfArray operator combined with $map within an aggregation pipeline. Syntax db.collection.aggregate([ { "$project": { "Result": { "$map": { ...

Read More

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 495 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 715 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 609 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 358 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
Showing 331–340 of 1,106 articles
« Prev 1 32 33 34 35 36 111 Next »
Advertisements