- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 1659 Articles for Big Data Analytics
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
198 Views
Use DISTINCT for distinct elements and then length to get the size of array −db.yourCollectionName.distinct('yourFieldName').length;Let us first create a collection with documents −> db.countOrSizeDemo.insertOne({"StudentFirstName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd304f5b64f4b851c3a13dc") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd304fab64f4b851c3a13dd") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd304fcb64f4b851c3a13de") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd30500b64f4b851c3a13df") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd30505b64f4b851c3a13e0") } > db.countOrSizeDemo.insertOne({"StudentFirstName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd3050ab64f4b851c3a13e1") }Following is the query to display all ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
1K+ Views
To rename a user, you need to use update() and $set to set the new username. Following is the syntax −db.system.users.update({"user":"yourOldUserName"}, {$set:{"user":"yourNewUserName"}});Firstly, display all the users from the MongoDB database −> use admin; switched to db admin > db.getUsers();This will produce the following output −[ { "_id" : "admin.Chris", "user" : "Chris", "db" : "admin", "roles" : [ { "role" : "readWrite", "db" : "test" } ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
2K+ Views
You can use $group operator along with aggregate framework to perform distinct on multiple fields. Let us first create a collection with documents −> db.distinctOnMultipleFieldsDemo.insertOne( ... { ... "StudentFirstName" : "Chris", ... "StudentAge" : 21, ... "StudentCountryName": "US" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd2e518b64f4b851c3a13d7") } > db.distinctOnMultipleFieldsDemo.insertOne( ... { ... "StudentFirstName" : "Robert", ... "StudentAge" : 21, ... "StudentCountryName": "AUS" ... } ... ); { "acknowledged" : true, "insertedId" ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
103 Views
Use dot notation(.) to search in inner classes using MongoDB. Let us first create a collection with documents −> db.searchInInnerDemo.insertOne( ... { ... "StudentFirstName" : "Robert", ... "StudentTechnicalDetails": ... { ... "StudentBackEndTechnology" : "MongoDB", ... "StudentLanguage" : "Java" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd2dd89b64f4b851c3a13d2") } > > db.searchInInnerDemo.insertOne( ... { ... "StudentFirstName" : "David", ... "StudentTechnicalDetails": ... { ... ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
363 Views
Use $in operator to get at least one match. Let us first create a collection with documents −> db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["MySQL", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2db5db64f4b851c3a13ce") } > db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["Java", "C", "MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2db71b64f4b851c3a13cf") } > db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["Python", "C++", "SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2db87b64f4b851c3a13d0") } >db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["Ruby", "Javascript", "C#", "MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2dba9b64f4b851c3a13d1") }Following is the query to display all documents from a collection with the help of find() method −> db.atleastOneMatchDemo.find().pretty();This will produce the following output −{ "_id" : ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
138 Views
To get a list of all databases, you need to use the below syntax −use admin db.runCommand({listDatabases: 1});To get a list of all collection names of a particular database, you need to use below syntax −use yourDatabaseName; db.getCollectionNames();Let us implement the above syntaxes −Case 1 − To get a list of databases> use admin switched to db admin > db.runCommand({listDatabases: 1});This will produce the following output −{ "databases" : [ { "name" : "admin", "sizeOnDisk" : 1675264, "empty" : false ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
253 Views
You can use $setIntersection operator along with aggregate framework to filter array elements in MongoDB. Let us first create a collection with documents −> db.filterArrayElementsDemo.insertOne( { "Scores": [10, 45, 67, 78, 90, 98, 99, 92] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd2d582b64f4b851c3a13c8") }Following is the query to display all documents from a collection with the help of find() method −> db.filterArrayElementsDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd2d582b64f4b851c3a13c8"), "Scores" : [ 10, 45, 67, 78, 90, ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
181 Views
It is up to you how much space you want. You need to use the parameter size to set. Use below syntax −db.createCollection(‘yourCollectionName’, capped=true, size=yourValue);Let us implement the above syntax in order to allow size for a capped collection −> db.createCollection('cappedCollectionMaximumSize', capped=true, size=1948475757574646); { "ok" : 1 }Let us check the description of the above collection −> db.cappedCollectionMaximumSize.find().explain();This will produce the following output −{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.cappedCollectionMaximumSize", "indexFilterSet" : false, "parsedQuery" : { }, "winningPlan" ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
206 Views
To set limit to $inc, use the below syntax −db.yourCollectionName.update({yourFieldName : {$lt : yourValue}}, {$inc : {yourFieldName : yourIncrementValue}}, false, true);Let us first create a collection with documents −> db.limitIncrementDemo.insertOne({"StudentId":101, "StudentScore":95}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2ce9eb64f4b851c3a13c3") } > db.limitIncrementDemo.insertOne({"StudentId":102, "StudentScore":55}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2cea0b64f4b851c3a13c4") } > db.limitIncrementDemo.insertOne({"StudentId":103, "StudentScore":67}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2cea1b64f4b851c3a13c5") } > db.limitIncrementDemo.insertOne({"StudentId":104, "StudentScore":56}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2cea3b64f4b851c3a13c6") } > db.limitIncrementDemo.insertOne({"StudentId":105, "StudentScore":79}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2cea4b64f4b851c3a13c7") }Following is the query to display all documents ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
1K+ Views
To retrieve values from nested JSON array, you can use the below syntax −db.yourCollectionName.find({"yourOuterFieldName.yourInnerFieldName.yourNextInnerFieldName…...N": "yourValue"}).pretty();Let us first create a collection with documents −> db.nestedJSONArrayDemo.insertOne({ ... "ClientDetails" : ... { ... "ClientPersonalDetails" : [ ... { "CountryName" : "US" }, ... { "CountryName" : "AUS"}, ... { "ClientName":"Chris" }, ... { "ClientName":"David" } ... ] ... } ... }); { "acknowledged" : true, "insertedId" : ObjectId("5cd2cbb2b64f4b851c3a13bc") } > db.nestedJSONArrayDemo.insertOne({ ... "ClientDetails" : ... ... Read More