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 106 of 111
How do I make case-insensitive queries on MongoDB?
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 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 MoreInclude all existing fields and add new fields to document in MongoDB?
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 MoreFind all duplicate documents in a MongoDB collection by a key field?
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 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 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 More