Articles on Trending Technologies

Technical articles with clear explanations and examples

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 132 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 281 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 574 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 408 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 340 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

How to create a performance system that count tags across a large dynamic dataset in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 136 Views

To create a performance system that counts tags across a large dynamic dataset in MongoDB, use indexes on array fields combined with aggregation pipelines and the explain() method to monitor query performance. Syntax // Create index on array field db.collection.createIndex({"arrayField": 1}); // Count tags with aggregation db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField", count: { $sum: 1 } } } ]); // Monitor performance db.collection.find(query).explain("executionStats"); Sample Data Let us create a collection with documents containing subject tags − db.demo278.createIndex({"Subjects": ...

Read More

Multi-key Indexing on an entire array with MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 148 Views

Multi-key indexing in MongoDB allows you to index entire arrays and their nested elements efficiently. When you create an index on an array field, MongoDB automatically creates a multi-key index that indexes each array element separately. Syntax db.collection.createIndex({ "arrayField": 1 }) db.collection.find({ "arrayField": { "nestedField": "value" } }) Create Sample Data db.demo277.insertMany([ { "details": [{ "FirstName": "John" }] }, { "details": [{ "FirstName": "David" }] }, { "details": [{ "FirstName": "Chris" }] } ]); { ...

Read More

Split a document by its subdocuments in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 665 Views

To split a document by its subdocuments, use $unwind in MongoDB. This operator deconstructs an array field from the input documents to output a document for each element. Syntax db.collection.aggregate([ { $unwind: "$arrayFieldName" } ]); Sample Data Let us create a collection with documents — db.demo276.insertOne({ "Name": "Chris", "Subjects": ["MySQL", "MongoDB"] }); { "acknowledged": true, "insertedId": ObjectId("5e48f953dd099650a5401a51") } Display all documents from a collection with the help ...

Read More

MongoDB query to skip first 5 records and display only the last 5 of them

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

To skip records in MongoDB, use skip(). With that, to display only a specific number of records, use limit(). Syntax db.collection.find().skip(numberOfRecordsToSkip).limit(numberOfRecordsToDisplay); Create Sample Data Let us create a collection with documents ? db.demo275.insertMany([ {"Number": 10}, {"Number": 12}, {"Number": 6}, {"Number": 1}, {"Number": 5}, {"Number": 24}, {"Number": 8}, {"Number": 9}, {"Number": 19}, {"Number": ...

Read More
Showing 23221–23230 of 61,297 articles
Advertisements