Articles on Trending Technologies

Technical articles with clear explanations and examples

MongoDB: nin and in not working together in $elemMatch to fetch documents having subjects "MongoDB", but not "Java

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 349 Views

To fetch documents containing "MongoDB" but not "Java" in an array field, use the $and operator with $in and $nin operators. The $elemMatch operator is not needed since we're checking the entire array for value presence. Syntax db.collection.find({ $and: [ { "arrayField": { $in: ["requiredValue"] } }, { "arrayField": { $nin: ["excludedValue"] } } ] }); Sample Data db.demo140.insertMany([ { "Id": 101, "Subjects": ["MongoDB", "MySQL"] ...

Read More

How to retrieve all nested fields from MongoDB collection?

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

To retrieve all nested fields from a MongoDB collection, use the $unwind aggregation operator to flatten arrays, then $project to extract specific nested fields into the document structure. Syntax db.collection.aggregate([ { $unwind: "$arrayFieldName" }, { $project: { "field1": "$arrayFieldName.nestedField1", "field2": "$arrayFieldName.nestedField2" }} ]); Sample Data db.demo138.insertMany([ { "Id": 101, ...

Read More

How to compare two fields in aggregation filter with MongoDB?

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

To compare two fields in an aggregation filter with MongoDB, use the $filter operator within a $project stage. This allows you to filter array elements based on field comparisons using the $eq operator. Syntax db.collection.aggregate([ { $project: { arrayField: { $filter: { ...

Read More

How to update array of subdocuments in MongoDB?

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

To update an array of subdocuments in MongoDB, use the update() method with the $set operator and the $ positional operator to target specific array elements. Syntax db.collection.update( { "field": "value", "arrayField.subField": "matchValue" }, { $set: { "arrayField.$.subField": "newValue" } } ); Sample Data db.demo134.insertMany([ { ...

Read More

Group by day/month/week based on the date range in MongoDB

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

To group documents by day, week, or month based on date ranges in MongoDB, use the $group stage with date operators like $week, $month, and $dayOfMonth in an aggregation pipeline. Syntax db.collection.aggregate([ { $project: { dateField: 1, week: { $week: "$dateField" }, month: { $month: "$dateField" }, ...

Read More

Group across two columns in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 289 Views

To group across two columns in MongoDB, use the $lookup operator to join documents based on matching field values, then perform aggregation operations on the combined data. Syntax db.collection.aggregate([ { "$lookup": { "from": "collection_name", "localField": "field1", "foreignField": "field2", ...

Read More

How to query MongoDB a value with lte, in and $not to fetch specific values?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 277 Views

To query MongoDB using $lte, $in, and $not operators together, you can combine these operators to fetch documents that meet specific criteria. These operators help filter documents based on comparison, inclusion, and negation conditions. Syntax db.collection.find({ "field": { "$lte": value, "$in": [value1, value2, ...], "$not": { "$gt": value } } }); Sample Data Let us first create a collection with documents ? ...

Read More

Get distinct values from a column in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 613 Views

To get distinct values from a column in MongoDB, use the distinct() method. This eliminates duplicate values and returns only unique values from the specified field. Syntax db.collection.distinct("fieldName") Sample Data Let us create a collection with documents that contain duplicate names ? db.demo128.insertMany([ {"Name": "Chris"}, {"Name": "David"}, {"Name": "David"}, {"Name": "Bob"}, {"Name": "Chris"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

How to add new item in nested array with MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 681 Views

To add a new item to nested array elements in MongoDB, use the $set operator with dot notation to target specific fields within array objects. You can also use JavaScript-based approaches for complex updates across all array elements. Syntax // Method 1: Update specific array element db.collection.updateOne( { query }, { $set: { "arrayName.index.newField": "value" } } ); // Method 2: Update all array elements using forEach db.collection.find().forEach(function(doc) { // Modify document db.collection.replaceOne({_id: doc._id}, doc); }); Sample Data ...

Read More

Search for documents with similar arrays in MongoDB and order by similarity value

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 275 Views

To search for documents with similar arrays in MongoDB and order by similarity value, use the aggregation pipeline with $unwind, $match, and $group stages to calculate similarity percentages based on matching array elements. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $match: { arrayField: { $in: targetArray } } }, { $group: { _id: "$_id", matches: { $sum: 1 } } }, { $project: { _id: 1, matches: 1, similarity: { $divide: ["$matches", targetArray.length] } } }, { ...

Read More
Showing 23241–23250 of 61,297 articles
Advertisements