MongoDB Articles

Page 79 of 111

MongoDB query to count the size of an array distinctly?

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

To count the size of an array distinctly in MongoDB, use the distinct() method to get unique elements and then apply .length to count them. This eliminates duplicate values and returns only the count of unique elements. Syntax db.collection.distinct('fieldName').length; Sample Data Let us create a collection with duplicate student names ? db.countOrSizeDemo.insertMany([ {"StudentFirstName": "John"}, {"StudentFirstName": "David"}, {"StudentFirstName": "David"}, {"StudentFirstName": "Carol"}, {"StudentFirstName": "Sam"}, {"StudentFirstName": "John"} ]); ...

Read More

How to rename a username in MongoDB?

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

To rename a username in MongoDB, you need to update the user document in the system.users collection using the $set operator. This operation modifies the user field while preserving all other user properties like roles and authentication mechanisms. Syntax db.system.users.update( {"user": "oldUserName"}, {$set: {"user": "newUserName"}} ); Display Current Users First, let's view all existing users in the database ? use admin; db.getUsers(); [ { "_id": "admin.Chris", ...

Read More

Performing distinct on multiple fields in MongoDB?

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

To perform distinct on multiple fields in MongoDB, use the $group operator with the aggregation framework. The $addToSet operator collects unique values for each field across all documents. Syntax db.collection.aggregate([ { $group: { _id: null, field1: { $addToSet: "$field1" }, field2: { $addToSet: "$field2" }, ...

Read More

How do I search according to fields in inner classes using MongoDB db.coll.find()?

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

Use dot notation (.) to search in inner classes (nested objects) using MongoDB. This allows you to query specific fields within embedded documents efficiently. Syntax db.collection.find({"outerField.innerField": "value"}) Sample Data db.searchInInnerDemo.insertMany([ { "StudentFirstName": "Robert", "StudentTechnicalDetails": { "StudentBackEndTechnology": "MongoDB", "StudentLanguage": "Java" } ...

Read More

Get at least one match in list querying with MongoDB?

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

To get at least one match in list querying with MongoDB, use the $in operator. This operator returns documents where the array field contains at least one of the specified values. Syntax db.collection.find({ "arrayField": { "$in": ["value1", "value2", "value3"] } }); Sample Data Let us create a collection with sample documents − db.atleastOneMatchDemo.insertMany([ {"StudentFavouriteSubject": ["MySQL", "MongoDB"]}, {"StudentFavouriteSubject": ["Java", "C", "MongoDB"]}, {"StudentFavouriteSubject": ["Python", "C++", "SQL Server"]}, {"StudentFavouriteSubject": ["Ruby", "Javascript", "C#", "MySQL"]} ]); ...

Read More

How to filter array elements in MongoDB?

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

To filter array elements in MongoDB, you can use the $setIntersection operator within the aggregation framework. This operator returns elements that exist in both the original array and your filter criteria array. Syntax db.collection.aggregate([ { $project: { fieldName: { $setIntersection: ["$arrayField", [filterValues]] } }} ]); Sample Data db.filterArrayElementsDemo.insertOne({ "Scores": [10, 45, 67, 78, ...

Read More

What is the MongoDB Capped Collection maximum allowable size?

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

MongoDB capped collections can be set to any size you specify using the size parameter. There is no fixed maximum limit − the size is constrained only by your available disk space and system resources. Syntax db.createCollection('collectionName', { capped: true, size: sizeInBytes }); Example Create a capped collection with a large size limit ? db.createCollection('cappedCollectionMaximumSize', { capped: true, size: 1948475757574646 }); { "ok" : 1 } Verify Collection Properties Check ...

Read More

How to set limit to $inc in MongoDB?

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

To set a limit to $inc in MongoDB, use a query condition with comparison operators like $lt to only increment values that meet specific criteria. This prevents incrementing values beyond a desired threshold. Syntax db.collection.updateMany( { fieldName: { $lt: limitValue } }, { $inc: { fieldName: incrementValue } } ); Create Sample Data db.limitIncrementDemo.insertMany([ { "StudentId": 101, "StudentScore": 95 }, { "StudentId": 102, "StudentScore": 55 }, { "StudentId": 103, "StudentScore": 67 }, ...

Read More

Retrieve values from nested JSON array in MongoDB?

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

To retrieve values from nested JSON array in MongoDB, use dot notation to navigate through the nested structure. This allows you to query specific fields within arrays and embedded documents. Syntax db.collectionName.find({ "outerField.arrayField.nestedField": "value" }); Sample Data Let us create a collection with nested JSON array documents ? db.nestedJSONArrayDemo.insertMany([ { "ClientDetails": { "ClientPersonalDetails": [ ...

Read More

Find and replace NumberLong type field in MongoDB?

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

To find and replace NumberLong type fields in MongoDB, use the $set operator with update() or updateMany(). This allows you to match documents containing specific NumberLong values and replace them with new NumberLong values. Syntax db.collection.update( { "fieldName": NumberLong(oldValue) }, { $set: { "fieldName": NumberLong(newValue) } }, { multi: true } ); Sample Data db.findAndReplaceDemo.insertMany([ { "UserId": NumberLong(101) }, { "UserId": NumberLong(110) }, { "UserId": NumberLong(101) }, ...

Read More
Showing 781–790 of 1,106 articles
« Prev 1 77 78 79 80 81 111 Next »
Advertisements