MongoDB Articles

Page 46 of 111

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

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 635 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 887 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 299 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

Min and max values of an array in MongoDB?

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

To find the minimum and maximum values from array elements in MongoDB, use the $min and $max operators within an aggregation pipeline. These operators work on array fields to extract the smallest and largest values respectively. Syntax db.collection.aggregate([ { $project: { "minField": { $min: "$arrayField.property" }, "maxField": { $max: "$arrayField.property" } } ...

Read More

What is the BSON query for the command 'show dbs' (list of databases) in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 128 Views

The BSON equivalent of the show dbs command in MongoDB is the listDatabases administrative command. This command must be run against the admin database to retrieve information about all databases on the MongoDB instance. Syntax db.runCommand({ listDatabases: 1 }) Example First, switch to the admin database ? use admin switched to db admin Now, run the listDatabases command ? db.runCommand({ listDatabases: 1 }) This will produce the following output ? { "databases": [ ...

Read More

MongoDB query to limit subdocuments having the given fields of the 'projection'

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 276 Views

To limit subdocuments having specific fields during projection in MongoDB, use the aggregation pipeline with $match, $unwind, and $project stages to filter and reshape the data. Syntax db.collection.aggregate([ { $match: { "arrayField.targetField": { $exists: 1 } } }, { $unwind: "$arrayField" }, { $match: { "arrayField.targetField": { $exists: 1 } } }, { $project: { "newField": "$arrayField.targetField", "_id": 0 } } ]); Sample Data db.demo285.insertOne({ details: [ ...

Read More

Export specified field of a collection in mongodb / mongodump to file?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 567 Views

To export specified fields from a MongoDB collection, use the mongoexport command with the -f parameter to specify which fields to export. This utility allows you to export data in various formats including CSV and JSON. Syntax mongoexport -d yourDatabaseName -c yourCollectionName -f yourFieldName --type=csv -o yourFileLocation/FileName Sample Data Let us create a collection with sample documents ? db.demo284.insertMany([ {"FirstName": "Chris"}, {"FirstName": "Robert"}, {"FirstName": "Bob"} ]); { "acknowledged": true, ...

Read More

MongoDB query to update array with another field?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 402 Views

To update an array with another field in MongoDB, use the $push operator along with $each to add multiple values at once. The $slice modifier can limit the array size. Syntax db.collection.update( {filter}, { $push: { arrayField: { $each: ["value1", "value2"], ...

Read More

How to delete element from an array in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 335 Views

To delete element from an array in MongoDB, use the $pull operator. This operator removes all instances of a specified value from an array field. Syntax db.collection.update( { query }, { $pull: { arrayField: valueToRemove } }, { multi: true } ); Create Sample Data Let us create a collection with documents − db.demo279.insertMany([ { id: [101, 103, 105, 110] }, { id: [107, 111, 110] } ]); { ...

Read More
Showing 451–460 of 1,106 articles
« Prev 1 44 45 46 47 48 111 Next »
Advertisements