- 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
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
599 Views
To get connected clients in MongoDB, use currentOp() with the set value to true and you need to iterate array result set with the help of field client. Let us first implement currentOp> db.currentOp(true)Following is the output. Here the client is 127.0.0.1 since we are using localhost. The output displays all the connected clients{ "inprog" : [ { "host" : "DESKTOP-QN2RB3H:27017", "desc" : "conn1", "connectionId" : 1, "client" : "127.0.0.1:61787", "appName" : "MongoDB Shell", ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
661 Views
You can use $slice operator to limit an array. Let us create a collection with documents. Following is the query> db.limitAnArrayDemo.insertOne( ... { ... _id: 101, ... "PlayerName": "Bob", ... "PlayerDetails": {Age:23, isStudent:true}, ... "PlayerGameScores": [234, 5767, 58, 67, 78, 90, 1002, 576, 68, 45, 23, 45, 678, 89, 78 ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 }Following is the query to display all documents from a collection with the help of find() method> db.limitAnArrayDemo.find().pretty();This will produce the following output{ ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
670 Views
To update only specific field, you can use $set operator. Let us first create a collection with documents>db.updateOnlySpecificFieldDemo.insertOne({"EmployeeName":"John", "EmployeeCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea849d628fa4220163b72") } >db.updateOnlySpecificFieldDemo.insertOne({"EmployeeName":"Larry", "EmployeeCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea853d628fa4220163b73") } >db.updateOnlySpecificFieldDemo.insertOne({"EmployeeName":"David", "EmployeeCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea85bd628fa4220163b74") }Following is the query to display all documents from a collection with the help of find() method> db.updateOnlySpecificFieldDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9ea849d628fa4220163b72"), "EmployeeName" : "John", "EmployeeCountryName" : "UK" } { "_id" : ObjectId("5c9ea853d628fa4220163b73"), "EmployeeName" : "Larry", "EmployeeCountryName" : ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
797 Views
In order to return only value of a field in MongoDB, you need to write a query and use forEach loop. Let us first create a collection with documents> db.returnOnlyValueOfFieldDemo.insertOne({"ClientName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea537d628fa4220163b6e") } > db.returnOnlyValueOfFieldDemo.insertOne({"ClientName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea53bd628fa4220163b6f") } > db.returnOnlyValueOfFieldDemo.insertOne({"ClientName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea541d628fa4220163b70") } > db.returnOnlyValueOfFieldDemo.insertOne({"ClientName":"Ramit"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea549d628fa4220163b71") }Following is the query to display all documents from a collection with the help of find() method> db.returnOnlyValueOfFieldDemo.find().pretty();This will produce the following output{ "_id" : ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
133 Views
You can use dot notation to get nested value. Let us first create a collection with documents> db.nestedQueryDemo.insertOne( ... { ... ... "EmployeeName" : "John", ... "EmployeeDetails" : ... { ... ... "_id":"EMP-101", ... "EmployeeAge":23, ... "EmployeeCompanyName":"IBM" ... ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c9ea31dd628fa4220163b69") } > db.nestedQueryDemo.insertOne( ... { ... ... "EmployeeName" : "Carol", ... "EmployeeDetails" : ... ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
161 Views
To get component of Date/ISODate in MongoDB, let us create a document with date in the collection. Now let us get the component of Date/ISODate in MongoDB> db.componentOfDateDemo.insert({"ShippingDate":new Date()}); WriteResult({ "nInserted" : 1 })Following is the query to display all documents from a collection with the help of find() method> db.componentOfDateDemo.find().pretty()This will produce the following output{ "_id" : ObjectId("5c9e9d57d628fa4220163b68"), "ShippingDate" : ISODate("2019-03-29T22:33:59.776Z") }Following is the query to get result using findOne()> var result=db.componentOfDateDemo.findOne();Now you can display documents from the collection. Following is the query> resultThis will produce the following output{ "_id" : ObjectId("5c9e9d57d628fa4220163b68"), "ShippingDate" : ISODate("2019-03-29T22:33:59.776Z") ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
511 Views
To find the document by field name with a specific value, you can use $exists operator. Let us create a collection with documents> db.findByFieldName.insertOne( { "Client":{ "ClientDetails":{ "ClientName":"Larry", "ClientAge":29 }, "ClientProjectDetails":{ "ProjectName":"Online Book Store", "TeamSize":10, "TechnologyUsed":"Spring Boot" } } } ); { "acknowledged" : true, "insertedId" : ObjectId("5c9e93b2d628fa4220163b64") } > db.findByFieldName.insertOne({ ... " Client":{ ... " ClientDetails":{ ... " ClientName":"Chris", ... " ClientAge":27 ... }, ... "ClientEducationDetails":{ ... " isEducated":true, ... "CollegeName":"M.I.T." ... ... ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
2K+ Views
There are two possibilities to check if MongoDB database exists.Case 1: The first possibility is that the MongoDB database exists i.e. it returns particular index.Case 2: The second possibility is that the MongoDB database does not exist i.e. it returns index -1.NOTE: An index starts from 0 and ends with (N-1) like an array.The syntax is as follows to check if MongoDB database exists.db.getMongo().getDBNames().indexOf("yourDatabaseName");Case 1: Let us implement the above syntax to check if MongoDB database exists. Following is the querydb.getMongo().getDBNames().indexOf("test");This will produce the following output6Look at the above sample output, we are getting 6 that means the database “test” ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
199 Views
To get MongoDB databases in a JavaScript array, you can use runCommand(). Following is the query to get MongoDB databases in a JavaScript array> use admin; switched to db admin > allDatabasesDetails = db.runCommand({listDatabases: 1});This will produce the following output{ "databases" : [ { "name" : "admin", "sizeOnDisk" : 847872, "empty" : false }, { "name" : "config", "sizeOnDisk" : 98304, "empty" : false ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
601 Views
In order to find record by _id in MongoDB, you can use the following syntaxdb.yourCollectionName.find({"_id":yourObjectId});Let us create a collection with documents> db.findRecordByIdDemo.insertOne({"CustomerName":"Larry", "CustomerAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c9dc2c875e2eeda1d5c3671") } > db.findRecordByIdDemo.insertOne({"CustomerName":"Bob", "CustomerAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c9dc2d175e2eeda1d5c3672") } > db.findRecordByIdDemo.insertOne({"CustomerName":"Carol", "CustomerAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c9dc2d775e2eeda1d5c3673") } > db.findRecordByIdDemo.insertOne({"CustomerName":"David", "CustomerAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9dc2e375e2eeda1d5c3674") }Following is the query to display all documents from a collection with the help of find() method> db.findRecordByIdDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9dc2c875e2eeda1d5c3671"), "CustomerName" ... Read More