- 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)
371 Views
To order by two fields sum, you can use the aggregate framework. Let us first create a collection with documents> db.orderByTwoFieldsDemo.insertOne({"Value1":10, "Value2":35}); { "acknowledged" : true, "insertedId" : ObjectId("5ca285576304881c5ce84baa") } > db.orderByTwoFieldsDemo.insertOne({"Value1":12, "Value2":5}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2855f6304881c5ce84bab") } > db.orderByTwoFieldsDemo.insertOne({"Value1":55, "Value2":65}); { "acknowledged" : true, "insertedId" : ObjectId("5ca285686304881c5ce84bac") }Following is the query to display all documents from a collection with the help of find() method> db.orderByTwoFieldsDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca285576304881c5ce84baa"), "Value1" : 10, "Value2" : 35 } { "_id" : ObjectId("5ca2855f6304881c5ce84bab"), "Value1" ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
178 Views
The MongoDB equivalent of WHERE IN(1, 2, ....) is $in operator. The syntax is as followsdb.yourCollectionName.find({yourFieldName:{$in:[yourValue1, yourValue2, ....N]}}).pretty();Let us first create a collection with documents> db.whereInDemo.insertOne({"StudentName":"John", "StudentMathScore":57}); { "acknowledged" : true, "insertedId" : ObjectId("5ca281ec6304881c5ce84ba5") } > db.whereInDemo.insertOne({"StudentName":"Larry", "StudentMathScore":89}); { "acknowledged" : true, "insertedId" : ObjectId("5ca281f56304881c5ce84ba6") } > db.whereInDemo.insertOne({"StudentName":"Chris", "StudentMathScore":98}); { "acknowledged" : true, "insertedId" : ObjectId("5ca281fd6304881c5ce84ba7") } > db.whereInDemo.insertOne({"StudentName":"Robert", "StudentMathScore":99}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2820a6304881c5ce84ba8") } > db.whereInDemo.insertOne({"StudentName":"Bob", "StudentMathScore":97}); { "acknowledged" : true, "insertedId" : ObjectId("5ca282206304881c5ce84ba9") }Following is the query to display all documents from a collection with ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
263 Views
To project specific fields from a document inside an array, you can use positional ($) operator.Let us first create a collection with documents> db.projectSpecificFieldDemo.insertOne( ... { ... "UniqueId": 101, ... "StudentDetails" : [{"StudentName" : "Chris", "StudentCountryName ": "US"}, ... {"StudentName" : "Robert", "StudentCountryName" : "UK"}, ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ca27aeb6304881c5ce84ba2") } > db.projectSpecificFieldDemo.insertOne( { "UniqueId": 102, "StudentDetails" : [{"StudentName" : "Robert", "StudentCountryName ": "UK"}, {"StudentName" : "David", "StudentCountryName" : "AUS"}, ] ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
2K+ Views
To query a key having space in its name, you can use dot(.) notation.Step 1: First, you need to create a set in which a key has space in its name. Following is the query:> myValues["Details"] = {} { } > myValues["Details"]["Student Name"]="John"; John > myValues["Details"]["StudentAge"]=26; 26Step 2: Now you need to create a collection and store the above set as a document. Following is the query> db.keyHavingSpaceDemo.insertOne( myValues); { "acknowledged" : true, "insertedId" : ObjectId("5ca27e3b6304881c5ce84ba4") }Following is the query to display all documents from a collection with the help of find() method> db.keyHavingSpaceDemo.find().pretty();This will produce the following ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
151 Views
To query an array string for a regexp match, use the following syntaxdb.yourCollectionName.find( { yourFieldName: /yourStartingValue./ } ).pretty();Let us first create a collection with documents> db.queryArrayDemo.insertOne({"StudentFullName":["Carol Taylor", "Caroline Williams", "Claire Brown"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2774c6304881c5ce84ba0") } > db.queryArrayDemo.insertOne({"StudentFullName":["John Smith", "Jace Doe", "Jabin Brown"]}); { "acknowledged" : true, "insertedId" : ObjectId("5ca277b36304881c5ce84ba1") }Following is the query to display all documents from a collection with the help of find() method> db.queryArrayDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca2774c6304881c5ce84ba0"), "StudentFullName" : [ "Carol Taylor", "Caroline Williams", ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
175 Views
To find and modify a value in a nested array, you can use update command. Let us first create a collection with documents> db.findAndModifyAValueInNestedArrayDemo.insertOne( { "CompanyName" : "Amazon", "DeveloperDetails" : [ { "ProjectName" : "Online Book Store", "TeamSize" : "5" }, { "ProjectName" : "Library Management System", "TeamSize" : "7" }, { "ProjectName" : "Online Banking Application", "TeamSize" : "15" } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5ca275226304881c5ce84b9f") }Following is the query to display all documents from a collection with the help of find() method> db.findAndModifyAValueInNestedArrayDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca275226304881c5ce84b9f"), ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
194 Views
You can use $abs operator for this. Let us first create a collection with documents> db.absoluteValueDemo.insert({"Value":98}); WriteResult({ "nInserted" : 1 }) > db.absoluteValueDemo.insert({"Value":-100}); WriteResult({ "nInserted" : 1 }) > db.absoluteValueDemo.insert({"Value":0}); WriteResult({ "nInserted" : 1 }) > db.absoluteValueDemo.insert({"Value":-9999990}); WriteResult({ "nInserted" : 1 })Following is the query to display all documents from a collection with the help of find() method:> db.absoluteValueDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca271f56304881c5ce84b9a"), "Value" : 98 } { "_id" : ObjectId("5ca271fa6304881c5ce84b9b"), "Value" : -100 } { "_id" : ObjectId("5ca271fe6304881c5ce84b9c"), "Value" : 0 } { "_id" : ObjectId("5ca2720f6304881c5ce84b9d"), "Value" : -9999990 }Following is the query to get ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
3K+ Views
To query on top N rows in MongoDB, you can use aggregate framework. Let us create a collection with documents> db.topNRowsDemo.insertOne({"StudentName":"Larry", "Score":78}); { "acknowledged" : true, "insertedId" : ObjectId("5ca26eee6304881c5ce84b91") } > db.topNRowsDemo.insertOne({"StudentName":"Chris", "Score":45}); { "acknowledged" : true, "insertedId" : ObjectId("5ca26ef66304881c5ce84b92") } > db.topNRowsDemo.insertOne({"StudentName":"Mike", "Score":65}); { "acknowledged" : true, "insertedId" : ObjectId("5ca26efe6304881c5ce84b93") } > db.topNRowsDemo.insertOne({"StudentName":"Adam", "Score":55}); { "acknowledged" : true, "insertedId" : ObjectId("5ca26f066304881c5ce84b94") } > db.topNRowsDemo.insertOne({"StudentName":"John", "Score":86}); { "acknowledged" : true, "insertedId" : ObjectId("5ca26f0f6304881c5ce84b95") }Following is the query to display all documents from a collection with the help of find() method> ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
2K+ Views
In order to print a variable at the MongoDB command prompt, use the following syntax//Declaring and Initializing a variable. var anyVariableName=yourValue; //To print the above variable. yourVariableName; Or print(yourVariableName);Following is how you can declare and initialize a variable at the MongoDB command prompt> var myIntegerValue=20;Print a variable at the MongoDB command prompt:> myIntegerValueThis will produce the following output20You can also use print(). Following is the query> print(myIntegerValue);This will produce the following output20Let us see another example. Following is the query to declare and initialize a string variable at MongoDB shell.> var myStringValue="Hello MongoDB!!!";Following is the query to print a variable ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
763 Views
To find all documents with a field that is NAN in MongoDB, use the following syntaxdb.yourCollectionName.find( { yourFieldName: NaN })Let us first create a collection with documents> db.nanDemo.insertOne({"Score":0/0}); { "acknowledged" : true, "insertedId" : ObjectId("5ca251a26304881c5ce84b8a") } > db.nanDemo.insertOne({"Score":10/5}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2520e6304881c5ce84b8b") } > db.nanDemo.insertOne({"Score":20/0}); { "acknowledged" : true, "insertedId" : ObjectId("5ca252156304881c5ce84b8c") } > db.nanDemo.insertOne({"Score":0/20}); { "acknowledged" : true, "insertedId" : ObjectId("5ca2521e6304881c5ce84b8d") }Following is the query to display all documents from a collection with the help of find() method> db.nanDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca251a26304881c5ce84b8a"), "Score" ... Read More