Anvi Jain

Anvi Jain

427 Articles Published

Articles by Anvi Jain

Page 14 of 43

How can I use 'Not Like' operator in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 1K+ Views

In MongoDB, there is no direct 'NOT LIKE' operator as found in SQL. Instead, use the $not operator combined with regular expressions to achieve similar functionality for pattern matching exclusion. Syntax db.collection.find({ fieldName: { $not: /pattern/ } }); Sample Data db.notLikeOperatorDemo.insertMany([ { "StudentName": "John Doe" }, { "StudentName": "John Smith" }, { "StudentName": "John Taylor" }, { "StudentName": "Carol Taylor" }, { "StudentName": "David Miller" } ]); ...

Read More

Querying an array of arrays in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 964 Views

Use the $elemMatch operator with nested $in to query an array of arrays in MongoDB. This approach allows you to search for specific values within nested array structures. Syntax db.collection.find({ "arrayField": { $elemMatch: { $elemMatch: { $in: ["searchValue"] } ...

Read More

How to remove an element from a doubly-nested array in a MongoDB document?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 2K+ Views

To remove an element from a doubly-nested array in MongoDB document, you can use $pull operator with proper dot notation to target the nested array location. Syntax db.collection.update( { "queryCondition": "value" }, { $pull: { "outerArray.index.innerArray": { "field": "valueToRemove" } } } ); Sample Data To understand the concept, let us create a collection with sample documents ? db.removeElementFromDoublyNestedArrayDemo.insertMany([ { "_id": "1", "UserName": "Larry", ...

Read More

How to convert string to numerical values in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 715 Views

To convert string values to numerical values in MongoDB, use forEach() with parseInt() or $toInt aggregation operator. The forEach() method iterates through documents and updates each string field to its numerical equivalent. Syntax db.collection.find().forEach(function(doc) { db.collection.update( { "_id": doc._id }, { "$set": { "fieldName": parseInt(doc.fieldName) } } ); }); Sample Data db.convertStringToNumberDemo.insertMany([ {"EmployeeId": "101", "EmployeeName": "Larry"}, {"EmployeeId": "1120", "EmployeeName": "Mike"}, ...

Read More

What is the $unwind operator in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 384 Views

The $unwind operator in MongoDB deconstructs an array field from input documents to output a document for each element. Each output document contains the same fields as the original document, but the array field is replaced with a single array element. Syntax db.collection.aggregate([ { $unwind: "$arrayField" } ]); Sample Data Let us create a collection with a document containing an array ? db.unwindOperatorDemo.insertOne({ "StudentName": "Larry", "StudentAge": 23, "StudentSubject": ["C", "C++", "Java", "MongoDB"] }); ...

Read More

Insert records in MongoDB collection if it does not exist?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 2K+ Views

You can use the update() function with the upsert option to insert records in MongoDB if they do not exist. When upsert is set to true, MongoDB will create a new document if no matching document is found. Syntax db.collection.update( { "field": "value" }, { "field": "value", "field2": "value2" }, { upsert: true } ); Create Sample Data db.insertIfNotExistsDemo.insertMany([ { "StudentName": "Mike", "StudentAge": 21 }, { "StudentName": "Sam", "StudentAge": 22 } ]); ...

Read More

Find value in a MongoDB Array with multiple criteria?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 577 Views

To find values in a MongoDB array with multiple criteria, use the $elemMatch operator combined with comparison operators like $gt and $lt. This allows you to match documents where at least one array element satisfies all specified conditions. Syntax db.collectionName.find({ arrayFieldName: { $elemMatch: { $gt: lowerValue, $lt: upperValue } ...

Read More

How to search in array of object in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 32K+ Views

To search in an array of objects in MongoDB, use the $elemMatch operator. This operator matches documents where at least one array element satisfies multiple query conditions within the same object. Syntax db.collection.find({ "arrayField": { $elemMatch: { "field1": "value1", "field2": "value2" } } }); Sample Data ...

Read More

How to perform descending order sort in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 459 Views

To sort documents in descending order in MongoDB, use the sort() method with -1 as the value for the field you want to sort by. Syntax db.yourCollectionName.find().sort({yourField: -1}); For ascending order, use 1 instead of -1 ? db.yourCollectionName.find().sort({yourField: 1}); Sample Data Let us create a collection with documents to demonstrate descending sort ? db.sortingDemo.insertMany([ {"Value": 100}, {"Value": 1}, {"Value": 150}, {"Value": 250}, {"Value": 5}, ...

Read More

MongoDB SELECT COUNT GROUP BY?

Anvi Jain
Anvi Jain
Updated on 14-Mar-2026 344 Views

To perform a SELECT COUNT GROUP BY operation in MongoDB, use the $group stage in the aggregation pipeline with the $sum accumulator to count documents grouped by a specific field. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", count: { $sum: 1 } } } ]); Sample Data Let's create a collection with student records to demonstrate counting by StudentId ? db.countGroupByDemo.insertMany([ {"StudentId": 10, "StudentName": "John"}, {"StudentId": 10, "StudentName": "Carol"}, {"StudentId": 20, "StudentName": "Sam"}, ...

Read More
Showing 131–140 of 427 articles
« Prev 1 12 13 14 15 16 43 Next »
Advertisements