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 79 of 111
MongoDB query to count the size of an array distinctly?
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 MoreHow to rename a username in MongoDB?
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 MorePerforming distinct on multiple fields in MongoDB?
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 MoreHow do I search according to fields in inner classes using MongoDB db.coll.find()?
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 MoreGet at least one match in list querying with MongoDB?
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 MoreHow to filter array elements in MongoDB?
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 MoreWhat is the MongoDB Capped Collection maximum allowable size?
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 MoreHow to set limit to $inc in MongoDB?
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 MoreRetrieve values from nested JSON array in MongoDB?
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 MoreFind and replace NumberLong type field in MongoDB?
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