- 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)
484 Views
To search for month and day only, use the aggregate framework.Let us first create a collection with documents −> db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2019-04-27")}); { "acknowledged" : true, "insertedId" : ObjectId("5cefbcaeef71edecf6a1f6b2") } > db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2018-04-30")}); { "acknowledged" : true, "insertedId" : ObjectId("5cefbcb7ef71edecf6a1f6b3") } > db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2018-04-27")}); { "acknowledged" : true, "insertedId" : ObjectId("5cefbcbcef71edecf6a1f6b4") } > db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2016-04-27")}); { "acknowledged" : true, "insertedId" : ObjectId("5cefbdaaef71edecf6a1f6b5") }Display all documents from a collection with the help of find() method −> db.monthAndDayDemo.find();Output{ "_id" : ObjectId("5cefbcaeef71edecf6a1f6b2"), "LoginDate" : ISODate("2019-04-27T00:00:00Z") } { "_id" : ObjectId("5cefbcb7ef71edecf6a1f6b3"), "LoginDate" : ISODate("2018-04-30T00:00:00Z") } { "_id" ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
238 Views
To limit number of values in a field, use $slice operator.Let us first create a collection with documents −> db.numberOfValuesDemo.insertOne({"Values":[100,200,300,900,1000,98]}); { "acknowledged" : true, "insertedId" : ObjectId("5cefb736ef71edecf6a1f6ab") }Display all documents from a collection with the help of find() method −> db.numberOfValuesDemo.find().pretty();Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 100, 200, 300, 900, 1000, 98 ] }Following is the query to limit number of values in a field using MongoDB −> db.numberOfValuesDemo.find({},{ "Values": { "$slice": 2 } } );Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 100, 200 ] }
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
71 Views
Let us first create a collection with documents −> db.numberOfValuesDemo.insertOne({"Values":[100,200,300,900,1000,98]}); { "acknowledged" : true, "insertedId" : ObjectId("5cefb736ef71edecf6a1f6ab") }Display all documents from a collection with the help of find() method −> db.numberOfValuesDemo.find().pretty();Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 100, 200, 300, 900, 1000, 98 ] }Following is the query to get the last two values.Here, we have used -ve sign under $slice −> db.numberOfValuesDemo.find({},{ "Values": { "$slice": -2 } } );Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 1000, 98 ] }
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
12K+ Views
To get last inserted document, use sort() along with limit(1).Let us first create a collection with documents −> db.getLastInsertedDocument.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefb17eef71edecf6a1f6a8") } > db.getLastInsertedDocument.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefb181ef71edecf6a1f6a9") } > db.getLastInsertedDocument.insertOne({"Name":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cefb185ef71edecf6a1f6aa") }Display all documents from a collection with the help of find() method −> db.getLastInsertedDocument.find();Output{ "_id" : ObjectId("5cefb17eef71edecf6a1f6a8"), "Name" : "John" } { "_id" : ObjectId("5cefb181ef71edecf6a1f6a9"), "Name" : "Chris" } { "_id" : ObjectId("5cefb185ef71edecf6a1f6aa"), "Name" : "Robert" }Following is the query to get last inserted document −> db.getLastInsertedDocument.find({}).sort({_id:-1}).limit(1);Output{ "_id" ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
448 Views
Let us first create a collection with documents −> db.findDataDemo.insertOne( { "_id": new ObjectId(), "CustomerName":"John", "CustomerDetails" : { "CountryName" : [ "AUS" ], "isMarried" : [ false ] } } ); { "acknowledged" : true, "insertedId" : ObjectId("5cefa5eeef71edecf6a1f6a5") } > db.findDataDemo.insertOne( { "_id": new ObjectId(), "CustomerName":"Carol", ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
104 Views
Use the distinct() for this, since it finds the distinct values for a specified field across a single collection or view and returns the results in an array.Let us first create a collection with documents −> db.projectionListDemo.insertOne({"_id":"1", "Subject":["MongoDB", "MySQL", "Java"]}); { "acknowledged" : true, "insertedId" : "1" } > db.projectionListDemo.insertOne({"_id":"2", "Subject":["MongoDB", "C", "C++"]}); { "acknowledged" : true, "insertedId" : "2" } > db.projectionListDemo.insertOne({"_id":"3", "Subject":["Java", "Python"]}); { "acknowledged" : true, "insertedId" : "3" }Display all documents from a collection with the help of find() method −> db.projectionListDemo.find().pretty();Output{ "_id" : "1", "Subject" : [ "MongoDB", "MySQL", "Java" ] } { "_id" : ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
131 Views
Let us first create a collection with documents -> db.removingAnArrayElementDemo.insertOne({"UserMessage":["Hi", "Hello", "Bye"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cef97bdef71edecf6a1f6a4") }Display all documents from a collection with the help of find() method -> db.removingAnArrayElementDemo.find().pretty();Output{ "_id" : ObjectId("5cef97bdef71edecf6a1f6a4"), "UserMessage" : [ "Hi", "Hello", "Bye" ] }Following is the query to remove an array element from MongoDB -> db.removingAnArrayElementDemo.update( {_id:ObjectId("5cef97bdef71edecf6a1f6a4")}, { "$pull": { "UserMessage": "Hello" } } ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Let us check the document once again:> db.removingAnArrayElementDemo.find().pretty();Output{ ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
66 Views
Let us first create a collection with documents -> db.operatorDemo.insertOne({"StudentSubject":["MongoDB", "MySQL", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cef94eaef71edecf6a1f6a2") } > db.operatorDemo.insertOne({"StudentSubject":["Java", "C", "C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cef94faef71edecf6a1f6a3") }Display all documents from a collection with the help of find() method -> db.operatorDemo.find().pretty();Output{ "_id" : ObjectId("5cef94eaef71edecf6a1f6a2"), "StudentSubject" : [ "MongoDB", "MySQL", "Java" ] } { "_id" : ObjectId("5cef94faef71edecf6a1f6a3"), "StudentSubject" : [ "Java", "C", "C++" ] }Following is the query for ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
4K+ Views
To fund a value from JSON data, use the find() along with dot(.) notation. Let us first create a collection with documents −> db.findValueFromJsonDemo.insertOne( { "UserDetails": [{ "_id": new ObjectId(), "UserName": "Carol", "UserMessage": "Hi" }], "UserFriendsName": ["John", "Sam"] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cdf8a4cbf3115999ed511fd") }Following is the query to display all documents from a collection with the help of find() method −> db.findValueFromJsonDemo.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)
208 Views
Use the $slice operator. Let us first create a collection with documents −> db.limitAndSliceProjectionDemo.insertOne( { "_id" : 101, "UserName" : "Carol", "UserAge" : 26, "UserMesssage" : [ "Hi", "Hello", "Bye", "Awesome", "Good", "Bad", "Nice", "Good Night", "Good Morning" ] } ); { "acknowledged" : true, "insertedId" : ... Read More