MongoDB Articles

Page 106 of 111

How do I make case-insensitive queries on MongoDB?

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

To make case-insensitive queries in MongoDB, use regular expressions with the i flag or the $regex operator. This allows you to match documents regardless of letter case variations. Syntax // Method 1: Regular Expression with i flag db.collection.find({ "field": /pattern/i }); // Method 2: $regex operator db.collection.find({ "field": { $regex: "pattern", $options: "i" } }); Sample Data db.caseInsensitiveDemo.insertMany([ {"UserName": "David"}, {"UserName": "DAVID"}, {"UserName": "david"}, {"UserName": "Carol"}, {"UserName": "Mike"}, ...

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

Include all existing fields and add new fields to document in MongoDB?

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

To include all existing fields and add new fields to a document in MongoDB, use the $addFields operator in an aggregation pipeline. This operator preserves all existing fields while adding new computed fields to the output. Syntax db.collection.aggregate([ { $addFields: { "newField1": "value1", "newField2": "$existingField", "newField3": { $multiply: ["$field1", ...

Read More

Find all duplicate documents in a MongoDB collection by a key field?

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

To find all duplicate documents in a MongoDB collection by a key field, use the aggregation framework with $group and $match stages to group by the field and filter groups with count greater than 1. Syntax db.collection.aggregate([ { $group: { _id: { fieldName: "$fieldName" }, documents: { $addToSet: "$_id" }, count: { $sum: 1 } }}, { $match: { count: { ...

Read More

How to convert string to numerical values in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 707 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 375 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 227 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 380 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 267 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
Showing 1051–1060 of 1,106 articles
« Prev 1 104 105 106 107 108 111 Next »
Advertisements