Articles on Trending Technologies

Technical articles with clear explanations and examples

MongoDB query to find value in array with multiple criteria (range)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 519 Views

To find values in an array within a specific range in MongoDB, use $elemMatch with $gt and $lt operators. This allows you to match documents where at least one array element satisfies multiple criteria. Syntax db.collection.find({ "arrayField": { "$elemMatch": { "field": { "$gt": minValue, ...

Read More

MongoDB findById returning a list of documents instead of a single result? How to get only a single document?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 283 Views

To get only a single result instead of a list when querying by ID in MongoDB, use the findOne() method instead of find(). The findOne() method returns exactly one document that matches your query criteria. Syntax db.collection.findOne({_id: ObjectId("id_value")}) Sample Data Let us create a collection with documents − db.demo340.insertMany([ {_id: 1, "Name": "Chris", Age: 21}, {_id: 2, "Name": "David", Age: 23}, {_id: 3, "Name": "Bob", Age: 20}, {_id: 4, "Name": "Sam", Age: 19} ]); ...

Read More

Zip two arrays and create new array of object in a reshaped form with MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 306 Views

To zip two arrays and create a new array of objects in MongoDB, use the $zip operator within an aggregation pipeline. This combines elements from multiple arrays at corresponding positions into a single reshaped structure. Syntax db.collection.aggregate([ { $project: { "newFieldName": { $map: { ...

Read More

Working with MongoDB concatArrays in project on existing multi-array field

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 432 Views

The $concatArrays operator is used to concatenate arrays and return the concatenated array. When working with multi-dimensional arrays, you can combine it with $reduce to flatten and concatenate nested arrays. Syntax { "$project": { "newFieldName": { "$reduce": { "input": "$arrayField", "initialValue": [], "in": { "$concatArrays": ["$$this", "$$value"] } } } ...

Read More

How to calculate sum in MongoDB with aggregate()?

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

To calculate sum in MongoDB, use the $sum operator along with the aggregate() method. The $sum operator is used within a $group stage to calculate the total of numeric field values across documents. Syntax db.collection.aggregate([ { $group: { _id: null, totalFieldName: { $sum: "$fieldName" } } } ]); ...

Read More

How to update only one property in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 507 Views

To update only one property in MongoDB, use the $set operator to modify a specific field without affecting other properties of the document. For arrays, use $addToSet or $push operators. Syntax db.collection.updateOne( { "field": "value" }, { $set: { "propertyToUpdate": "newValue" } } ); Sample Data db.demo336.insertMany([ { "Name": "Chris", "Score": [45, 67, 78] }, { "Name": "David", "Score": [89, 93, 47] } ]); { "acknowledged": true, ...

Read More

MongoDB query to convert a string with commas to double

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 849 Views

To convert a string with commas to a double value in MongoDB, use the aggregate pipeline with $split, $reduce, and $convert operators to remove commas and convert the result to a numeric type. Syntax db.collection.aggregate([ { $project: { convertedField: { $convert: { ...

Read More

MongoDB query to add matched key to list after query?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 157 Views

To add matched elements to a list after querying in MongoDB, use the $filter operator within an aggregation pipeline. This allows you to filter array elements based on specific conditions and return only the matching elements. Syntax db.collection.aggregate([ { $addFields: { arrayField: { $filter: { ...

Read More

Create an index for text search in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 219 Views

To create an index for text search in MongoDB, use the createIndex() method with the "text" index type. This enables full-text search capabilities on string fields using the $text operator. Syntax db.collection.createIndex({ fieldName: "text" }); db.collection.find({ $text: { $search: "searchTerm" } }); Sample Data Let us create a collection with sample documents: db.demo331.insertMany([ { "Words": "This is a MySQL" }, { "Words": "THIS is a MongoDB" } ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Restrict returned data in MongoDB to display only specific values from documents

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 447 Views

To restrict returned data in MongoDB, use the projection parameter in the find() method. Use values 1 to include fields and 0 to exclude fields from the query results. Syntax db.collection.find(query, projection) Where: query − Filter criteria (empty {} for all documents) projection − Fields to include (1) or exclude (0) Sample Data Let us create a collection with documents − db.demo330.insertMany([ {"Id": 101, "Name": "Chris", "Age": 21}, {"Id": 102, "Name": "Sam", "Age": 24}, {"Id": 103, "Name": ...

Read More
Showing 23151–23160 of 61,297 articles
Advertisements