Nishtha Thakur

Nishtha Thakur

398 Articles Published

Articles by Nishtha Thakur

Page 12 of 40

Update object at specific Array Index in MongoDB?

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

To update object at specific array index in MongoDB, use the $set operator with dot notation to target the exact position in nested arrays. Specify the array path with numeric indices to access objects at specific locations. Syntax db.collection.update( { "field": "value" }, { $set: { "arrayName.index1.index2.fieldName": "newValue" } } ); Sample Data Let us first create a collection with nested array structure: db.updateObjectDemo.insertOne({ id: 101, "StudentDetails": [ ...

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

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

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 238 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

Variable collection name in MongoDB shell with JavaScript?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 276 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 to remove key fields in MongoDB?

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

To remove key fields in MongoDB, you can use the $unset operator. This operator deletes specified fields from documents in a collection. Syntax db.collection.updateMany( { query }, { $unset: { fieldName: 1 } } ); Sample Data Let us first create a collection with sample documents ? db.removeKeyFieldsDemo.insertMany([ { "StudentFirstName": "John", "StudentLastName": "Doe", "StudentAge": 23 ...

Read More

How to maintain the top count of array elements in MongoDB?

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

To maintain the top count of array elements in MongoDB, use the aggregation framework with $unwind to flatten arrays, $group to count frequencies, $sort to order by count, and $limit to get the top N elements. Syntax db.collection.aggregate([ { $unwind: "$arrayField" }, { $group: { _id: "$arrayField", count: { $sum: 1 } } }, { $sort: { count: -1 } }, { $limit: N } ]); Sample Data db.topCountArrayDemo.insertMany([ { "StudentId": 101, "StudentSubject": ...

Read More

How do I check whether a field contains null value in MongoDB?

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

To check whether a field contains a null value in MongoDB, use the $type operator with type number 10 (which represents null) or compare the field directly with null. Syntax // Method 1: Using $type operator db.collection.find({ fieldName: { $type: 10 } }); // Method 2: Direct null comparison db.collection.find({ fieldName: null }); Sample Data Let us create a collection with documents, including one with a null field ? db.nullDemo.insertMany([ { "FirstName": "Chris" }, { "FirstName": null }, { ...

Read More

Get all the column names in a table in MongoDB

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

In MongoDB, there is no concept of columns since MongoDB is schema-less and does not contain tables. It contains the concept of collections and a collection has different types of documents with varying field structures. Syntax // Insert documents with different field structures db.collectionName.insertOne({"fieldName1": "value1", "fieldName2": "value2"}); // Get all field names from a single document db.collectionName.findOne(); // Get all documents to see different field structures db.collectionName.find(); Sample Data Let us create a collection with documents having different field structures ? db.collectionOnDifferentDocumentDemo.insertMany([ {"UserName": "Larry"}, ...

Read More

Loop through all MongoDB collections and execute query?

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

To loop through all MongoDB collections and execute a query, use db.getCollectionNames() to retrieve collection names, then iterate using forEach() to perform operations on each collection. Syntax db.getCollectionNames().forEach(function(collectionName) { // Execute your query on each collection var result = db[collectionName].find(query); // Process the result }); Example: Get Latest Document from Each Collection Loop through all collections and get the timestamp of the most recent document ? db.getCollectionNames().forEach(function(collectionName) { var latest = db[collectionName].find().sort({_id:-1}).limit(1); ...

Read More
Showing 111–120 of 398 articles
« Prev 1 10 11 12 13 14 40 Next »
Advertisements