Database Articles

Page 109 of 547

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

MongoDB find by multiple array items using $in?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 242 Views

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 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

How can I rename a field for all documents in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 394 Views

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 More

How to get a particular element from MongoDB array?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 268 Views

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 More

How to create MongoDB stored procedure?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 1K+ Views

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 More

Find value in a MongoDB Array with multiple criteria?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 578 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

Check if a field contains a string in MongoDB?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 2K+ Views

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 More

Find Strings greater than a particular length in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 2K+ Views

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
Showing 1081–1090 of 5,468 articles
« Prev 1 107 108 109 110 111 547 Next »
Advertisements