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
Articles by Anvi Jain
Page 14 of 43
How can I use 'Not Like' operator in MongoDB?
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 MoreQuerying an array of arrays in MongoDB?
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 MoreHow to remove an element from a doubly-nested array in a MongoDB document?
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 MoreHow to convert string to numerical values in MongoDB?
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 MoreWhat is the $unwind operator in MongoDB?
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 MoreInsert records in MongoDB collection if it does not exist?
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 MoreFind value in a MongoDB Array with multiple criteria?
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 MoreHow to search in array of object in MongoDB?
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 MoreHow to perform descending order sort in MongoDB?
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 MoreMongoDB SELECT COUNT GROUP BY?
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