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 107 of 111
How to create MongoDB stored procedure?
MongoDB allows you to create stored procedures (server-side JavaScript functions) using the db.system.js collection. These functions are stored on the server and can be called using db.eval(). Syntax db.system.js.save({ _id: "yourStoredProcedueName", value: function(argument1, argument2, ...argumentN) { // function body return result; } }); Example: Create Addition Function Let's create a stored procedure that adds two numbers ? db.system.js.save({ _id: "addTwoValue", ...
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 MoreCheck if a field contains a string in MongoDB?
You can use the $regex operator to check if a field contains a string in MongoDB. This allows pattern matching and substring searches within document fields. Syntax db.collection.find({ "fieldName": { $regex: ".*searchString.*" } }); Sample Data db.checkFieldContainsStringDemo.insertMany([ { "Id": 1, "Name": "John" }, { "Id": 2, "Name": "Johnson" }, { "Id": 3, "Name": "Carol" }, { "Id": 4, "Name": "Mike" }, { "Id": 5, "Name": "Sam" }, { "Id": ...
Read MoreFind Strings greater than a particular length in MongoDB?
To find strings with a length greater than a particular value in MongoDB, use the $where operator with JavaScript expressions to access the string's length property. Syntax db.collectionName.find({ $where: 'this.fieldName.length > value' }); Sample Data Let us create a collection with user documents to demonstrate string length filtering ? db.stringFieldLengthDemo.insertMany([ {"UserId": 1, "UserName": "Adam Smith"}, {"UserId": 2, "UserName": "Carol Taylor"}, {"UserId": 3, "UserName": "James Brown"}, {"UserId": 4, "UserName": "John Smith"}, ...
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 copy a collection from one database to another in MongoDB?
In MongoDB, there is no built-in command to copy a collection from one database to another. To achieve this, use the find().forEach() method combined with getSiblingDB() to iterate through documents and insert them into the destination database. Syntax db.collectionName.find().forEach(function(doc){ db.getSiblingDB('destinationDatabase')['collectionName'].insert(doc); }); Create Sample Data First, let's create a collection in the test database with some documents ? use test; db.userCollection.insertMany([ {"User_Id": 101, "UserName": "Larry"}, {"User_Id": 102, "UserName": "Maxwell"}, {"User_Id": 103, "UserName": "Robert"} ]); ...
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 MoreHow to perform ascending order sort in MongoDB?
To sort documents in ascending order in MongoDB, use the sort() method with a field value of 1. This arranges documents from lowest to highest based on the specified field. Syntax db.collectionName.find().sort({fieldName: 1}); Sample Data Let us create a collection with sample documents to demonstrate sorting ? db.sortingDemo.insertMany([ {"Value": 100}, {"Value": 1}, {"Value": 150}, {"Value": 250}, {"Value": 5}, {"Value": 199}, {"Value": 243}, ...
Read MoreGet distinct record values in MongoDB?
The distinct() method in MongoDB returns unique values for a specified field across all documents in a collection, automatically removing duplicates. Syntax db.collectionName.distinct("fieldName"); Sample Data db.distinctRecordDemo.insertMany([ {"StudentId": 1, "StudentName": "John", "StudentAge": 21}, {"StudentId": 2, "StudentName": "John", "StudentAge": 22}, {"StudentId": 3, "StudentName": "Carol", "StudentAge": 21}, {"StudentId": 4, "StudentName": "Carol", "StudentAge": 26}, {"StudentId": 5, "StudentName": "Sam", "StudentAge": 24}, {"StudentId": 6, "StudentName": "Mike", "StudentAge": 27}, {"StudentId": 7, "StudentName": ...
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