- 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 1349 Articles for MongoDB
![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)
796 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)
509 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
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
82 Views
To improve querying field in MongoDB, you need to use index. Let us create a collection with documents> db.improveQueryDemo.insertOne( ... { ... "PlayerDetails":[ ... {"PlayerName": "John", "PlayerGameScore": 5690}, ... {"PlayerName": "Carol", "PlayerGameScore": 2690}, ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c9dbaf875e2eeda1d5c3670") }Following is the query to display all documents from a collection with the help of find() method> db.improveQueryDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9dbaf875e2eeda1d5c3670"), "PlayerDetails" : [ { "PlayerName" : "John", ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
2K+ Views
In MySQL, we give an alias name for a column. Similarly, you can give an alias name for field name in MongoDB. The MongoDB equivalent syntax is as followsdb.yourCollectionName.aggregate( [ { "$project": { "_id": 0, "anyAliasName": "$yourFieldName" }} ]);Let us first create a collection with documents> db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d448827b86948e204ca91") } > db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d449027b86948e204ca92") } > db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d449527b86948e204ca93") } > db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9d449927b86948e204ca94") ... Read More