Articles on Trending Technologies

Technical articles with clear explanations and examples

MongoDB query to find records with keys containing dots?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 889 Views

To find MongoDB records containing field names with dots, use the aggregation pipeline with $addFields, $objectToArray, and $filter operators. The $indexOfBytes operator detects dot characters in field names. Syntax db.collection.aggregate([ { $addFields: { result: { $filter: { ...

Read More

Fetch multiple documents in MongoDB query with OR condition?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 220 Views

To fetch multiple documents in MongoDB using OR condition, use the $or operator which performs a logical OR operation on an array of expressions and selects documents that satisfy at least one of the expressions. Syntax db.collection.find({ $or: [ { field1: value1 }, { field2: value2 }, { field3: value3 } ] }); Sample Data db.demo362.insertMany([ { ...

Read More

Using object as key for finding values in MongoDB

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

To use an object field as a key for finding values in MongoDB, use dot notation to access nested object properties in the projection parameter of the find() method. Syntax db.collection.find({}, {"objectName.fieldName": 1}); Sample Data Let us create a collection with nested object documents − db.demo361.insertMany([ {"details": {"FirstName": "Chris", "LastName": "Brown"}}, {"details": {"FirstName": "David", "LastName": "Miller"}}, {"details": {"FirstName": "John", "LastName": "Doe"}} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

Find current and previous documents in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 719 Views

To get the current and previous documents in MongoDB, use sort() with limit() to retrieve the most recent documents based on a specific criteria. This approach helps you find the latest two records that match your query conditions. Syntax db.collection.find( { field: { $lte: value } }, { projection } ).sort({ _id: -1 }).limit(2); Sample Data Let us create a collection with sample documents: db.demo360.insertMany([ { id: 101, "Name": "Chris" }, { id: 102, "Name": "David" ...

Read More

MongoDB query to concatenate values of array with other fields

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

To concatenate values of an array with other fields in MongoDB, use $reduce to convert the array into a single string, then $concat to combine it with other fields in the $project stage. Syntax db.collection.aggregate([ { $project: { arrayAsString: { $reduce: { ...

Read More

Does aggregation query with $match works in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 205 Views

Yes, the $match operator works perfectly in MongoDB aggregation queries. It filters documents in the aggregation pipeline, similar to the find() method's query criteria, but as the first stage of data processing. Syntax db.collection.aggregate([ { $match: { "field": "value" } }, // Additional pipeline stages... ]); Sample Data db.demo358.insertMany([ { "ClientId": 101, "ClientName": "Chris", "ClientAge": 34 }, { "ClientId": 102, "ClientName": "David", "ClientAge": 32 }, { "ClientId": 103, "ClientName": "David", "ClientAge": 35 }, ...

Read More

MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 323 Views

To fetch a specific document from MongoDB where a field value is set using NumberInt(), use dot notation to target nested fields and match the exact NumberInt() value in your query. Syntax db.collection.find({"field.nestedField": NumberInt(value)}); Sample Data Let us create a collection with documents containing NumberInt() values − db.demo357.insertMany([ { "FirstName": "Chris", "Age": 21, "details": { ...

Read More

How to validate documents before insert or update in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 678 Views

MongoDB provides built-in document validation to enforce data quality rules before inserting or updating documents. Use the validator option during collection creation to define validation criteria using MongoDB query operators. Syntax db.createCollection("collectionName", { validator: { $jsonSchema: { /* validation rules */ } // OR /* MongoDB query expression */ }, validationLevel: "strict", // or "moderate" validationAction: "error" ...

Read More

How to project specific elements in an array field with MongoDB?

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

To project specific elements in an array field in MongoDB, use the $project stage with $filter to conditionally include array elements that match your criteria. Syntax db.collection.aggregate([ { $project: { arrayField: { $filter: { input: "$arrayField", ...

Read More

MongoDB query for counting number of unique fields grouped by another field?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 373 Views

To count the number of unique field combinations grouped by another field in MongoDB, use the aggregation pipeline with $group stages. First group by the unique combinations, then group again by the target field to count occurrences. Syntax db.collection.aggregate([ { $group: { _id: { "groupField": "$groupField", ...

Read More
Showing 23131–23140 of 61,297 articles
Advertisements