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
Database Articles
Page 109 of 547
How 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 MoreMongoDB find by multiple array items using $in?
To find documents by multiple array items in MongoDB, use the $in operator. This operator matches documents where the array field contains any of the specified values. Syntax db.collection.find({ "arrayField": { $in: ["value1", "value2", "value3"] } }); Sample Data db.findByMultipleArrayDemo.insertMany([ { "StudentFirstName": "John", "StudentLastName": "Smith", "StudentCoreSubject": ["Compiler", "Operating System", "Computer Networks"] }, { ...
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 MoreHow can I rename a field for all documents in MongoDB?
To rename a field for all documents in MongoDB, use the $rename operator with an empty query filter to match all documents. This operation updates the field name across the entire collection. Syntax db.collectionName.updateMany( {}, { $rename: { "oldFieldName": "newFieldName" } } ); Sample Data Let's create a collection with student documents ? db.renameFieldDemo.insertMany([ { "StudentName": "John" }, { "StudentName": "Carol" }, { "StudentName": "Bob" }, { "StudentName": ...
Read MoreHow to get a particular element from MongoDB array?
To get a particular element from a MongoDB array, use the $arrayElemAt operator within the aggregation framework. This operator allows you to extract an element at a specific index position from an array field. Syntax db.collection.aggregate([ { $project: { fieldName: { $arrayElemAt: ["$arrayField", index] } } } ]); Sample Data db.getParticularElement.insertOne({ "InstructorName": "Larry", ...
Read MoreHow 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 More