Articles on Trending Technologies

Technical articles with clear explanations and examples

Get distinct levels of array field in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 250 Views

To get distinct levels of array field in MongoDB, use the $addToSet operator within an aggregation pipeline. This operator collects unique values and prevents duplicates when grouping array fields. Syntax db.collection.aggregate([ { $group: { "_id": null, "fieldName": { $addToSet: "$arrayField" } } } ]); Sample Data ...

Read More

MongoDB query to implement aggregate function

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 259 Views

MongoDB aggregate functions allow you to perform data processing operations on collections, such as filtering, grouping, and transforming documents. The aggregation pipeline processes documents through multiple stages to produce computed results. Syntax db.collection.aggregate([ { $match: { field: value } }, { $unwind: "$arrayField" }, { $group: { _id: "$field", operation: { $operator: expression } } } ]) Sample Data Let us first create a collection with documents ? db.demo121.insertOne({ "Id": 101, "Details": ...

Read More

MongoDB query for ranking / search count?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 490 Views

To implement ranking or search count in MongoDB, use the aggregation pipeline with $setIntersection to find matching elements and $divide to calculate a relevance score based on the ratio of matched terms to total document elements. Syntax db.collection.aggregate([ { "$match": { "fieldName": { "$in": searchArray } } }, { "$addFields": { "rankScore": { "$divide": [ ...

Read More

Select special fields rather than all in MongoDB

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 188 Views

To select specific fields instead of all fields in MongoDB, use field projection in the find() method. Set unwanted fields to 0 to exclude them, or set wanted fields to 1 to include only those fields. Syntax db.collection.find({}, {field1: 0, field2: 0}); // Exclude fields db.collection.find({}, {field1: 1, field2: 1}); // Include only these fields Sample Data db.demo269.insertMany([ {StudentId: 101, StudentSubject: "MySQL"}, {StudentId: 102, StudentSubject: "Java"}, {StudentId: 103, StudentSubject: "MongoDB"}, {StudentId: 104, StudentSubject: "C"} ]); ...

Read More

Use MongoDB Aggregate and select only top record (descending)

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 291 Views

To select only the top record in descending order using MongoDB aggregate, use the $sort stage to order documents and the $limit stage to restrict the result to one document. Syntax db.collection.aggregate([ { $sort: { fieldName: -1 } }, { $limit: 1 } ]); Sample Data db.demo267.insertMany([ { id: 100, "Name": "Chris" }, { id: 100, "Name": "Adam" }, { id: 100, "Name": "David" }, { id: 100, "Name": ...

Read More

How to query with nand operator in MongoDB?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 309 Views

MongoDB doesn't have a $nand operator, but you can achieve NAND (Not AND) logic by using $or with $ne operators. This returns documents where at least one condition is false. Syntax db.collection.find({ $or: [ { "field1": { $ne: value1 } }, { "field2": { $ne: value2 } } ] }); Sample Data db.demo266.insertMany([ { "active1": true, "active2": false }, { "active1": ...

Read More

MongoDB query to update only certain fields?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 286 Views

To update only certain fields in MongoDB, use the $set operator. This operator modifies specific fields without affecting other existing fields in the document. Syntax db.collection.update( { "field": "value" }, { $set: { "fieldToUpdate": "newValue" } } ); Sample Data Let us create a collection with sample documents − db.demo265.insertMany([ {"id": 101, "Name": "Chris"}, {"id": 102, "Name": "Bob"}, {"id": 103, "Name": "David"} ]); { "acknowledged": ...

Read More

Using count equivalent in MongoDB to find top users with max occurrence

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 524 Views

To find the top users with maximum occurrence in MongoDB, use the $group stage with aggregate() to count occurrences, then $sort and $limit to get the top results. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", count: { $sum: 1 } } }, { $sort: { count: -1 } }, { $limit: numberOfTopResults } ]); Sample Data Let us create a collection with user documents ? db.demo264.insertMany([ { "Name": "Chris" }, { "Name": ...

Read More

MongoDB query to skip documents

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 254 Views

To skip documents in MongoDB, use the skip() method. This method allows you to skip a specified number of documents from the beginning of the result set, useful for pagination and result navigation. Syntax db.collection.find().skip(number) Sample Data db.demo263.insertMany([ {_id: 100}, {_id: 200}, {_id: 300} ]); { "acknowledged": true, "insertedIds": { "0": 100, "1": 200, "2": 300 } } ...

Read More

Invert Result of MongoDB Query (Implement Opposite of $and operation)?

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 302 Views

To invert the result of a MongoDB query (opposite of $and operation), use $or combined with $ne operators. This applies De Morgan's Law: NOT (A AND B) = (NOT A) OR (NOT B). Syntax db.collection.find({ $or: [ { "field1": { $ne: "value1" } }, { "field2": { $ne: "value2" } } ] }); Sample Data db.demo4.insertMany([ { uid: 1, "Name": "Chris", "Age": 22 }, ...

Read More
Showing 23251–23260 of 61,297 articles
Advertisements