- 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
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
126 Views
Capped collections are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. In order to set max on mongo capped collection, use the following syntaxdb.createCollection("yourCollectionName", {capped:true, size:yourSizeValue, max:yourMaxValue});Let us implement the above syntax in order to set max on mongo capped collection. Following is the query> db.createCollection("setMaxDemo", {capped:true, size:948575757, max:2000});This will produce the following output{ "ok" : 1 }You can now get all information about the above collection> db.setMaxDemo.stats();This will produce the following output{ "ns" : "test.setMaxDemo", "size" : 0, "count" : 0, "storageSize" : 4096, "capped" : true, ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
158 Views
You can use $in operator instead of $elemMatch on first level array. The syntax is as followsdb.yourCollectionName.find({yourFieldName:{$in:["yourValue"]}}).pretty();Let us first create a collection with documents>db.firstLevelArrayDemo.insertOne({"StudentName":"Chris", "StudentTechnicalSkills":["Mongo DB", "MySQL", "SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2360f66324ffac2a7dc71") } >db.firstLevelArrayDemo.insertOne({"StudentName":"Robert", "StudentTechnicalSkills":["C", "J ava", "C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2362766324ffac2a7dc72") }Following is the query to display all documents from a collection with the help of find() method> db.firstLevelArrayDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca2360f66324ffac2a7dc71"), "StudentName" : "Chris", "StudentTechnicalSkills" : [ "MongoDB", "MySQL", "SQL ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
3K+ Views
To retrieve the first document in a collection, you can use findOne(). Following is the syntaxvar anyVariableName=db.yourCollectionName.findOne(); //To print result at MongoDB console write the variable name yourVariableNameLet us first create a collection with documents> db.retrieveFirstDocumentDemo.insertOne({"ClientName":"Robert", "ClientAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2325966324ffac2a7dc6d") } > db.retrieveFirstDocumentDemo.insertOne({"ClientName":"Chris", "ClientAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2326266324ffac2a7dc6e") } > db.retrieveFirstDocumentDemo.insertOne({"ClientName":"Larry", "ClientAge":29}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2326a66324ffac2a7dc6f") } > db.retrieveFirstDocumentDemo.insertOne({"ClientName":"David", "ClientAge":39}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2327566324ffac2a7dc70") }Following is the query to display all documents from a collection with the help of ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
669 Views
For find() to search for nested keys in MongoDB, you can use dot(.) notation. Following is the syntaxdb.yourCollectionName.find({"yourOuterFieldName.yourInnerFieldName":"yourValue"}).pretty();Let us first create a collection with documents:>db.searchForNestedKeysDemo.insertOne({"ClientName":"Larry", "ClientAge":28, "ClientExtra Details":{"isEducated":true, "CountryName":"US"}}); { "acknowledged" : true, "insertedId" : ObjectId("5ca20e8b66324ffac2a7dc64") } >db.searchForNestedKeysDemo.insertOne({"ClientName":"Chris", "ClientAge":29, "ClientExtra Details":{"isEducated":false, "CountryName":"UK"}}); { "acknowledged" : true, "insertedId" : ObjectId("5ca20ea366324ffac2a7dc65") } >db.searchForNestedKeysDemo.insertOne({"ClientName":"David", "ClientAge":39, "ClientExtra Details":{"isEducated":true, "CountryName":"AUS"}}); { "acknowledged" : true, "insertedId" : ObjectId("5ca20eba66324ffac2a7dc66") }Following is the query to display all documents from a collection with the help of find() method> db.searchForNestedKeysDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca20e8b66324ffac2a7dc64"), "ClientName" : "Larry", ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
480 Views
To prevent MongoDB from returning the Object ID while finding a document, you need to set _id to 0. Let us first create a collection with documents> db.preventObjectIdDemo.insertOne( ... { ... ... "StudentName" : "Chris", ... "StudentDetails" : [ ... { ... "StudentTotalScore" : 540, ... "StudentCountryName" : "US" ... }, ... { ... "StudentTotalScore" : 489, ... ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
670 Views
To convert from String to date data type, you need to write some script. Let us first create a collection with documents>db.stringToDateDataTypeDemo.insertOne({"CustomerName":"Carol", "ShippingDate":"2019- 01-21"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2071d66324ffac2a7dc60") } >db.stringToDateDataTypeDemo.insertOne({"CustomerName":"Bob", "ShippingDate":"2019- 02-24"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2073566324ffac2a7dc61") } >db.stringToDateDataTypeDemo.insertOne({"CustomerName":"Chris", "ShippingDate":"2019- 04-01"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2074266324ffac2a7dc62") }Following is the query to display all documents from a collection with the help of find() method> db.stringToDateDataTypeDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca2071d66324ffac2a7dc60"), "CustomerName" : "Carol", "ShippingDate" : "2019-01-21" } { "_id" : ObjectId("5ca2073566324ffac2a7dc61"), ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
220 Views
To find inside a hash MongoDB, you can use dot(.) notation. Let us first create a collection with documents> db.hashDemo.insertOne({"ClientName":"Larry", "ClientAge":23, "ClientDetails":{ "isEducated": true, "ClientProject" : "University Management"}}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1ef1266324ffac2a7dc5e") } > db.hashDemo.insertOne({"ClientName":"Chris", "ClientAge":26, "ClientDetails":{ "isEducated":false, "ClientProject" : "Online Book Store"}}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1ef7766324ffac2a7dc5f") }Following is the query to display all documents from a collection with the help of find() method> db.hashDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca1ef1266324ffac2a7dc5e"), "ClientName" : "Larry", "ClientAge" : 23, "ClientDetails" : { "isEducated" ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
584 Views
You can use $lte operator along with new Date() for this. Let us first create a collection with documents>db.dateTimeValueLessThanNowDemo.insertOne({"CustomerName":"Larry", "CustomerProductName":"Product-1", "ArrivalDate":new ISODate("2017-01-31")}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e8ab66324ffac2a7dc59") } >db.dateTimeValueLessThanNowDemo.insertOne({"CustomerName":"Mike", "CustomerProductName":"Product-2", "ArrivalDate":new ISODate("2019-04-01")}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e8c166324ffac2a7dc5a") } >db.dateTimeValueLessThanNowDemo.insertOne({"CustomerName":"Chris", "CustomerProductName":"Product-3", "ArrivalDate":new ISODate("2019-03-31")}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e8d266324ffac2a7dc5b") } >db.dateTimeValueLessThanNowDemo.insertOne({"CustomerName":"Robert", "CustomerProductName":"Product-4", "ArrivalDate":new ISODate("2019-04-02")}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e8e766324ffac2a7dc5c") }Following is the query to display all documents from a collection with the help of find() method> db.dateTimeValueLessThanNowDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca1e8c166324ffac2a7dc5a"), ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
665 Views
You can use update command along with forEach() for large collection. Let us first create a collection with documents>db.addingNewPropertyDemo.insertOne({"StudentName":"John", "StudentAge":23, "CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e61866324ffac2a7dc56") } >db.addingNewPropertyDemo.insertOne({"StudentName":"David", "StudentAge":21, "CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e62366324ffac2a7dc57") } >db.addingNewPropertyDemo.insertOne({"StudentName":"Bob", "StudentAge":21, "CountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e62d66324ffac2a7dc58") }Following is the query to display all documents from a collection with the help of find() method> db.addingNewPropertyDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca1e61866324ffac2a7dc56"), "StudentName" : "John", "StudentAge" : 23, "CountryName" : "US" } { "_id" : ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
385 Views
In order to terminate a MongoDB shell script earlier, you need to use quit. Following is the syntaxquit() quit(1)Let us create a script and try to write quit() or quit(1) in shell. At first, we will create the following collection with documents> db.flightInformation.insertOne({"FlightName":"Flight-1", "ArrivalTime":new ISODate("2019-03-12")}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e341e941bbb0e8bf5639") } > db.flightInformation.insertOne({"FlightName":"Flight-2", "ArrivalTime":new ISODate("2019-03-31")}); { "acknowledged" : true, "insertedId" : ObjectId("5ca1e351e941bbb0e8bf563a") }Following is the query to display all documents from a collection with the help of find() method> db.flightInformation.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca1e341e941bbb0e8bf5639"), "FlightName" : "Flight-1", ... Read More