- 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
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
859 Views
You can use remove() for this. Let us first create a collection with documents −> db.deleteDataDemo.insertOne({_id:1, "Name":"Larry"}); { "acknowledged" : true, "insertedId" : 1 } > db.deleteDataDemo.insertOne({_id:2, "Name":"Chris"}); { "acknowledged" : true, "insertedId" : 2 } > db.deleteDataDemo.insertOne({_id:3, "Name":"Robert"}); { "acknowledged" : true, "insertedId" : 3 } > db.deleteDataDemo.insertOne({_id:4, "Name":"David"}); { "acknowledged" : true, "insertedId" : 4 } > db.deleteDataDemo.insertOne({_id:5, "Name":"Carol"}); { "acknowledged" : true, "insertedId" : 5 } > db.deleteDataDemo.insertOne({_id:6, "Name":"Sam"}); { "acknowledged" : true, "insertedId" : 6 }Following is the query to display all documents from a collection with the help of find() method −> db.deleteDataDemo.find().pretty();This will produce ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
1K+ Views
To query for multiple parameters in MongoDB, you can use the dot(.) notation. Let us first create a collection with documents −> db.multipleParametersDemo.insertOne( ... { ... "CustomerName" : "Larry", ... "CustomerDetails" : [ ... { ... "CustomerCountryName" : "US", ... "CustomerBankName" : "HDFC", ... "CustomerBalance" : 17363, ... } ... ], ... "Purchase" : 1456, ... ... } ... ); ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
122 Views
Use the $pull operator to pull array element from a collection. Let us first create a collection with documents −> db.pullElementFromAnArrayDemo.insertOne( ... { ... "StudentScores":[89, 56, 78, 90] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd0104a588d4a6447b2e063") }Following is the query to display all documents from a collection with the help of find() method −> db.pullElementFromAnArrayDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd0104a588d4a6447b2e063"), "StudentScores" : [ 89, 56, 78, 90 ] }Following is the query to pull array element from a collection. Here, we are removing element 78 −> ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
277 Views
You can use update command to add a field to existing record. Let us first create a collection with documents −> db.addAFieldToEveryRecordDemo.insertOne({"ClientName":"Chris", "ClientAge":34}); { "acknowledged" : true, "insertedId" : ObjectId("5cd00e32588d4a6447b2e061") } > db.addAFieldToEveryRecordDemo.insertOne({"ClientName":"Robert", "ClientAge":36}); { "acknowledged" : true, "insertedId" : ObjectId("5cd00e59588d4a6447b2e062") }Following is the query to display all documents from a collection with the help of find() method −> db.addAFieldToEveryRecordDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd00e32588d4a6447b2e061"), "ClientName" : "Chris", "ClientAge" : 34 } { "_id" : ObjectId("5cd00e59588d4a6447b2e062"), "ClientName" : "Robert", "ClientAge" : 36 }Here is the query ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
284 Views
To query null value in MongoDB, use $ne operator. Let us first create a collection with documents −> db.queryingNullDemo.insertOne( ... { ... "StudentName":"Larry", ... "StudentDetails": ... { ... "StudentAge":21, ... "StudentSubject":"MongoDB" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd00bec588d4a6447b2e05f") } > db.queryingNullDemo.insertOne( ... { ... "StudentName":"Sam", ... "StudentDetails": ... { ... "StudentAge":23, ... ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
1K+ Views
The statement db.collection.find() returns the cursor of Result Set of a query by which you can iterate over the result set or print all documents.Let us first create a collection with documents −> db.findCursorDemo.insertOne({"ClientFirstName":"John", "ClientLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd00a1c588d4a6447b2e05c") } > db.findCursorDemo.insertOne({"ClientFirstName":"Carol", "ClientLastName":"Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd00a26588d4a6447b2e05d") } > db.findCursorDemo.insertOne({"ClientFirstName":"David", "ClientLastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd00a33588d4a6447b2e05e") }Following is the query to display all documents from a collection with the help of find() method −> db.findCursorDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd00a1c588d4a6447b2e05c"), "ClientFirstName" : ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
697 Views
You can work with aggregate framework and use $sort, $skip and $limit to display distinctly ordered records with skip and also set the limit. Let us first create a collection with documents> db.orderedDistinctDemo.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb8e0140b992277dae0e9") } > db.orderedDistinctDemo.insertOne({"Name":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb8e5140b992277dae0ea") } > db.orderedDistinctDemo.insertOne({"Name":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb8e7140b992277dae0eb") } > db.orderedDistinctDemo.insertOne({"Name":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb8ea140b992277dae0ec") } > db.orderedDistinctDemo.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb8ee140b992277dae0ed") } > db.orderedDistinctDemo.insertOne({"Name":"Carol"}); { "acknowledged" : true, "insertedId" ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
380 Views
Use drop() to delete a table. Following is the syntax −db.yourCollectionName.drop();Let us first create a collection with documents −> db.deleteTableDemo.insertOne({"Name":"Chris", "Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb705140b992277dae0e6") } > db.deleteTableDemo.insertOne({"Name":"Carol", "Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb70c140b992277dae0e7") } > db.deleteTableDemo.insertOne({"Name":"David", "Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb714140b992277dae0e8") }Following is the query to display all documents from a collection with the help of find() method −> db.deleteTableDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5ccfb705140b992277dae0e6"), "Name" : "Chris", "Age" : 23 } { "_id" : ObjectId("5ccfb70c140b992277dae0e7"), ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
133 Views
For MongoDB collection beginning with_, following is the syntax −db.createCollection(‘_yourCollectionName’);Insert query using below syntax −db.getCollection('_yourCollectionName').insertOne({"yourFieldName1":"yourValue1", "yourFieldName2":yourValue2, ............N});Let us first create a collection with documents −> db.createCollection('_testUnderscoreCollectionDemo'); { "ok" : 1 } >db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"John", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb4a6140b992277dae0e4") } >db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"Carol", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb4af140b992277dae0e5") }Following is the query to display all documents from a collection with the help of find() method −> db.getCollection('_testUnderscoreCollectionDemo').find().pretty();This will produce the following output −{ "_id" : ObjectId("5ccfb4a6140b992277dae0e4"), "StudentFirstName" : "John", "StudentAge" : 23 } { "_id" : ObjectId("5ccfb4af140b992277dae0e5"), ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
870 Views
To retrieve a nested object in MongoDB, use $ operator. Let us first create a collection with documents −> db.queryNestedObject.insertOne( ... { ... "StudentName" : "James", ... "StudentSubjectScore" : [ ... {"StudentMongoDBScore":98}, ... {"StudentCScore":92}, ... {"StudentJavaScore":91} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ccf49a9dceb9a92e6aa1962") }Following is the query to display all documents from a collection with the help of find() method −> db.queryNestedObject.find().pretty();This will produce the following output −{ ... Read More