Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
MongoDB Articles
Page 49 of 111
How to query MongoDB a value with lte, in and $not to fetch specific values?
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 MoreGet distinct values from a column in MongoDB?
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 MoreHow to add new item in nested array with MongoDB?
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 MoreSearch for documents with similar arrays in MongoDB and order by similarity value
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 MoreGet distinct levels of array field in MongoDB?
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 MoreMongoDB query to implement aggregate function
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 MoreMongoDB query for ranking / search count?
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 MoreSelect special fields rather than all in MongoDB
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 MoreUse MongoDB Aggregate and select only top record (descending)
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 MoreHow to query with nand operator in MongoDB?
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