- 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
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
250 Views
Following is the syntax to use $regex in MongoDB −db.yourCollectionName.find({yourFieldName: { $regex: yourValue}});Let us first create a collection with documents −> db.regularExpressionDemo.insertOne({"UserName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cdffc25bf3115999ed51210") } > db.regularExpressionDemo.insertOne({"UserName":"JOHN"}); { "acknowledged" : true, "insertedId" : ObjectId("5cdffc2ebf3115999ed51211") } > db.regularExpressionDemo.insertOne({"UserName":"john"}); { "acknowledged" : true, "insertedId" : ObjectId("5cdffc35bf3115999ed51212") } > db.regularExpressionDemo.insertOne({"UserName":"JoHn"}); { "acknowledged" : true, "insertedId" : ObjectId("5cdffc3ebf3115999ed51213") }Following is the query to display all documents from a collection with the help of find() method −> db.regularExpressionDemo.find();This will produce the following output −{ "_id" : ObjectId("5cdffc25bf3115999ed51210"), "UserName" : "John" } { ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
250 Views
As an alternative, use the $eq operator. Let us first create a collection with documents −> db.equalDemo.insertOne({_id:1, "StudentFriendNames":["John", "Carol", "Sam"]}); { "acknowledged" : true, "insertedId" : 1 } > db.equalDemo.insertOne({_id:2, "StudentFriendNames":null}); { "acknowledged" : true, "insertedId" : 2 } > db.equalDemo.insertOne({_id:3, "StudentFriendNames":["Carol"]}); { "acknowledged" : true, "insertedId" : 3 } > db.equalDemo.insertOne({_id:4, "StudentFriendNames":["Sam"]}); { "acknowledged" : true, "insertedId" : 4 }Following is the query to display all documents from a collection with the help of find() method −> db.equalDemo.find();This will produce the following output −{ "_id" : 1, "StudentFriendNames" : [ "John", "Carol", "Sam" ] } { "_id" : 2, ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
294 Views
Use $or operator along with $expr operator for this. Let us first create a collection with documents, wherein one of fields is isMarried having true of false value −> db.orTwoFieldsDemo.insertOne({"isLiveInUS":true, "isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfd86abf3115999ed5120d") } > db.orTwoFieldsDemo.insertOne({"isLiveInUS":true, "isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfd876bf3115999ed5120e") } > db.orTwoFieldsDemo.insertOne({"isLiveInUS":false, "isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfd87dbf3115999ed5120f") }Following is the query to display all documents from a collection with the help of find() method −> db.orTwoFieldsDemo.find();This will produce the following output −{ "_id" : ObjectId("5cdfd86abf3115999ed5120d"), "isLiveInUS" : true, "isMarried" : false ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
253 Views
To get the minimum value, use sort() along with limit(1). Let us first create a collection with documents −> db.findMinimumValueDemo.insertOne({"Value":1004}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfd61abf3115999ed51208") } > db.findMinimumValueDemo.insertOne({"Value":983}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfd61ebf3115999ed51209") } > db.findMinimumValueDemo.insertOne({"Value":990}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfd622bf3115999ed5120a") } > db.findMinimumValueDemo.insertOne({"Value":1093}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfd626bf3115999ed5120b") } > db.findMinimumValueDemo.insertOne({"Value":10090}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfd62fbf3115999ed5120c") }Following is the query to display all documents from a collection with the help of find() method −> db.findMinimumValueDemo.find();This will produce the following ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
342 Views
Let us first create a collection with documents −> db.queryForPartialObjectDemo.insertOne({_id:new ObjectId(), "StudentDetails": [{"StudentId":1, "StudentName":"Chris"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfcf55bf3115999ed51206") } > db.queryForPartialObjectDemo.insertOne({_id:new ObjectId(), "StudentDetails": [{"StudentId":2, "StudentName":"David"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfcf55bf3115999ed51207") }Following is the query to display all documents from a collection with the help of find() method −> db.queryForPartialObjectDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cdfcf55bf3115999ed51206"), "StudentDetails" : [ { "StudentId" : 1, "StudentName" : "Chris" } ] } { "_id" ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
138 Views
Let us first create a collection with documents −> dbmongoDBCollectionSizeDemoinsertOne({"Name":"John", "Age":23}); { "acknowledged" : true, "insertedId" : ObjectId("5cf23e3db64a577be5a2bc16") } > dbmongoDBCollectionSizeDemoinsertOne({"Name":"Chris", "Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5cf23e45b64a577be5a2bc17") } > dbmongoDBCollectionSizeDemoinsertOne({"Name":"Robert", "Age":26}); { "acknowledged" : true, "insertedId" : ObjectId("5cf23e4db64a577be5a2bc18") }Following is the query to display all documents from a collection with the help of find() method −> dbmongoDBCollectionSizeDemofind();This will produce the following document −{ "_id" : ObjectId("5cf23e3db64a577be5a2bc16"), "Name" : "John", "Age" : 23 } { "_id" : ObjectId("5cf23e45b64a577be5a2bc17"), "Name" : "Chris", "Age" : 24 } { "_id" : ObjectId("5cf23e4db64a577be5a2bc18"), "Name" : "Robert", "Age" ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
127 Views
For this, use the $exists operator. Let us first create a collection with documents −>dbfindDocumentContainsSpecificFieldDemoinsertOne({"ProductPrices":{"Product1":10, "Pr oduct2":50}}); { "acknowledged" : true, "insertedId" : ObjectId("5cf2385bb64a577be5a2bc14") } >dbfindDocumentContainsSpecificFieldDemoinsertOne({"ProductPrices":{"Product3":150, "P roduct7":100, "Product5":250}}); { "acknowledged" : true, "insertedId" : ObjectId("5cf2387eb64a577be5a2bc15") }Following is the query to display all documents from a collection with the help of find() method −> dbfindDocumentContainsSpecificFieldDemofind()pretty();This will produce the following document −{ "_id" : ObjectId("5cf2385bb64a577be5a2bc14"), "ProductPrices" : { "Product1" : 10, "Product2" : 50 } } { "_id" : ObjectId("5cf2387eb64a577be5a2bc15"), "ProductPrices" : { ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
314 Views
Use $slice operator for this. Let us first create a collection with documents −> db.gettingFirstItemInArrayDemo.insertOne( { "UserId": 101, "UserName":"Carol", "UserOtherDetails": [ {"UserFriendName":"Sam"}, {"UserFriendName":"Mike"}, {"UserFriendName":"David"}, {"UserFriendName":"Bob"} ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cdfca52bf3115999ed51205") }Following is the query to display all documents from a collection with the help of find() method −> db.gettingFirstItemInArrayDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cdfca52bf3115999ed51205"), "UserId" : 101, ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
109 Views
For this, use MongoDB $substr. Let us first create a collection with documents −> dblimitTheNumberOfCharactersDemoinsertOne({"Title":"MongoDB is No SQL database"}); { "acknowledged" : true, "insertedId" : ObjectId("5cf23013b64a577be5a2bc0e") } > dblimitTheNumberOfCharactersDemoinsertOne({"Title":"MySQL is a relational database"}); { "acknowledged" : true, "insertedId" : ObjectId("5cf2302db64a577be5a2bc0f") } Following is the query to display all documents from a collection with the help of find() method −> dblimitTheNumberOfCharactersDemofind()pretty();This will produce the following document −{ "_id" : ObjectId("5cf23013b64a577be5a2bc0e"), "Title" : "MongoDB is No SQL database" } { "_id" : ObjectId("5cf2302db64a577be5a2bc0f"), "Title" : "MySQL is a relational database" }Following is the ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
186 Views
You can use pop() for this. Let us first create a collection with documents −> db.persistChangeDemo.insertOne({"Name" : "Larry", "CreditScore": [500, 700, 760, 100]}); { "acknowledged" : true, "insertedId" : ObjectId("5cdfc52cbf3115999ed51203") }Following is the query to display all documents from a collection with the help of find() method −> db.persistChangeDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cdfc52cbf3115999ed51203"), "Name" : "Larry", "CreditScore" : [ 500, 700, 760, 100 ] }Following is the query to pop a value −> myDocument.CreditScore.pop(); 100Let us save ... Read More