MongoDB Articles

Page 83 of 111

What is the syntax for boolean values in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 6K+ Views

MongoDB supports native boolean values true and false. You can query boolean fields using equality operators like $eq and $ne, or by direct value matching. Syntax // Direct boolean matching db.collection.find({"booleanField": true}); db.collection.find({"booleanField": false}); // Using $eq operator db.collection.find({"booleanField": {$eq: true}}); // Using $ne operator db.collection.find({"booleanField": {$ne: false}}); Sample Data Let us create a collection with boolean values − db.booleanQueryDemo.insertMany([ {"UserName": "John", "UserAge": 23, "isMarried": true}, {"UserName": "Chris", "UserAge": 21, "isMarried": false}, {"UserName": "Robert", "UserAge": 24, ...

Read More

Remove and update the existing record in MongoDB?

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

To remove and update existing records in MongoDB, use the $pull operator which removes elements from an array that match specified criteria. This operator effectively updates the document by removing the matching array elements. Syntax db.collection.update( { "filter_condition": "value" }, { "$pull": { "arrayField": { "field": "valueToRemove" } } } ); Sample Data Let us create a collection with documents containing user details in an array ? db.removeDemo.insertMany([ { "UserName": "Larry", ...

Read More

Iterate a cursor and print a document in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 328 Views

To iterate a cursor and print documents in MongoDB, use the forEach() method with printjson. This approach allows you to process each document individually and format the output for better readability. Syntax db.collection.find().forEach(printjson); Create Sample Data Let's create a collection with student documents ? db.cursorDemo.insertMany([ {"StudentFullName": "John Smith", "StudentAge": 23}, {"StudentFullName": "John Doe", "StudentAge": 21}, {"StudentFullName": "Carol Taylor", "StudentAge": 20}, {"StudentFullName": "Chris Brown", "StudentAge": 24} ]); { "acknowledged": true, ...

Read More

Find all collections in MongoDB with specific field?

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

To find all collections in MongoDB that contain documents with a specific field, use getCollectionNames() combined with the $exists operator to check for field presence across all collections in the database. Syntax db.getCollectionNames().forEach(function(collectionName) { var count = db[collectionName].find({"fieldName": {$exists: true}}).count(); if (count > 0) { print(collectionName); } }); Example: Find Collections with "StudentFirstName" Field The following query searches all collections for documents containing the "StudentFirstName" field ? db.getCollectionNames().forEach(function(myCollectionName) { ...

Read More

Find in a dictionary like structure by value with MongoDB?

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

To find documents in a dictionary-like structure by value in MongoDB, use dot notation to specify the exact path to the nested field you want to search. Syntax db.collection.find({ "parentObject.nestedObject.fieldName": "searchValue" }); Sample Data Let us first create a collection with nested dictionary-like documents : db.findInDictionaryDemo.insertMany([ { "_id": 101, "AllCustomerDetails": { "SomeCustomerDetail1": { ...

Read More

Regex to ignore a specific character in MongoDB?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 1K+ Views

To ignore a specific character in MongoDB, use regular expressions with either the $not operator or character negation patterns. These approaches filter out documents containing the unwanted character. Syntax // Method 1: Using character negation db.collection.find({field: /^[^CHARACTER]*$/}); // Method 2: Using $not operator db.collection.find({field: {$not: /CHARACTER/}}); Sample Data db.regexDemo.insertMany([ {"CustomerId": "Customer#1234", "CustomerName": "Chris"}, {"CustomerId": "Customer5678", "CustomerName": "Robert"}, {"CustomerId": "Customer#777", "CustomerName": "Carol"}, {"CustomerId": "Customer777", "CustomerName": "David"} ]); { "acknowledged": true, ...

Read More

How do you perform an AND query on an array in MongoDB?

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

To perform an AND query on an array in MongoDB, use the $all operator. This operator matches documents where the array field contains all the specified elements, regardless of order or additional elements. Syntax db.collection.find({ "arrayField": { $all: ["value1", "value2", "value3"] } }); Sample Data db.andQueryDemo.insertMany([ { "StudentName": "Carol Taylor", "FavouriteSubject": ["C", "Java", "MongoDB", "MySQL"] }, { ...

Read More

Check if value exists for a field in a MongoDB document?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 2K+ Views

To check if a value exists for a field in a MongoDB document, you can use the $exists operator with the find() method. This operator checks whether a field exists and optionally whether it contains any values. Syntax db.collection.find({ "fieldName": { $exists: true } }); db.collection.find({ "arrayField.0": { $exists: true } }); Sample Data db.checkIfValueDemo.insertMany([ { "PlayerName": "John Smith", "PlayerScores": [5000, 98595858, 554343] }, { ...

Read More

Variable collection name in MongoDB shell with JavaScript?

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

Yes, you can set a variable collection name in MongoDB shell using JavaScript. This allows you to reference collections through variables instead of typing the full collection name repeatedly. Syntax var variableName = db.collectionName; variableName.find(); Sample Data Let us first create a collection with documents ? db.employeeInformation.insertOne({ "EmployeeName": "John Smith", "EmployeeAge": 24, "EmployeeSalary": 450000 }); { "acknowledged": true, "insertedId": ObjectId("5cc6d06baf8e7a4ca6b2ad97") } Display all documents from the collection ...

Read More

How do I know which MongoDB version is installed using the Command Line?

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

To check which MongoDB version is installed on your system, you can use the command line to query the MongoDB server directly. This is useful for verifying your installation and ensuring compatibility with your applications. Syntax mongo --version Or alternatively: mongod --version Method 1: Using mongo --version Open your command prompt and navigate to the MongoDB bin directory. Then execute the following command ? mongo --version MongoDB shell version v4.0.5 git version: 3739429dd92b92d1b0ab120911a23d50bf03c412 OpenSSL version: OpenSSL 1.1.0i 14 Aug 2018 allocator: tcmalloc modules: none ...

Read More
Showing 821–830 of 1,106 articles
« Prev 1 81 82 83 84 85 111 Next »
Advertisements