- 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
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
146 Views
To search a value, simply use $where in MongoDB. Let us create a collection with documents −> db.demo268.insertOne( ... { ... "details" : { ... "101" : "John", ... "1001" : "Bob" ... } ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4816141627c0c63e7dbaaf") }Display all documents from a collection with the help of find() method −> db.demo268.find();This will produce the following output −{ "_id" : ObjectId("5e4816141627c0c63e7dbaaf"), "details" : { "101" : "John", "1001" : "Bob" } }Following is the query to search a ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
174 Views
For descending order, use -1, which specifies sorting order for sort(), Let us create a collection with documents −> db.demo267.insertOne({id:100, "Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4811951627c0c63e7dbaab") } > db.demo267.insertOne({id:100, "Name":"Adam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e48119e1627c0c63e7dbaac") } > db.demo267.insertOne({id:100, "Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4811a51627c0c63e7dbaad") } > db.demo267.insertOne({id:100, "Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4811ab1627c0c63e7dbaae") }Display all documents from a collection with the help of find() method −> db.demo267.find().pretty(); { "_id" : ObjectId("5e4811951627c0c63e7dbaab"), "id" : 100, "Name" : "Chris" } { "_id" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
188 Views
The $not operator won’t invert a complex expression. Therefore, use $and or $or with $ne operator.Let us create a collection with documents −> db.demo266.insertOne({"active1":true, "active2":false}); { "acknowledged" : true, "insertedId" : ObjectId("5e480f4b1627c0c63e7dbaa7") } > db.demo266.insertOne({"active1":true, "active2":true}); { "acknowledged" : true, "insertedId" : ObjectId("5e480f501627c0c63e7dbaa8") } > db.demo266.insertOne({"active1":false, "active2":false}); { "acknowledged" : true, "insertedId" : ObjectId("5e480f561627c0c63e7dbaa9") } > db.demo266.insertOne({"active1":false, "active2":true}); { "acknowledged" : true, "insertedId" : ObjectId("5e480f701627c0c63e7dbaaa") }Display all documents from a collection with the help of find() method −> db.demo266.find();This will produce the following output −{ "_id" : ObjectId("5e480f4b1627c0c63e7dbaa7"), "active1" : true, "active2" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
178 Views
To update only certain fields, use $set. Let us create a collection with documents −> db.demo265.insertOne({"id":101, "Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e480d781627c0c63e7dbaa4") } > db.demo265.insertOne({"id":102, "Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e480d7d1627c0c63e7dbaa5") } > db.demo265.insertOne({"id":103, "Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e480d841627c0c63e7dbaa6") }Display all documents from a collection with the help of find() method −> db.demo265.find();This will produce the following output −{ "_id" : ObjectId("5e480d781627c0c63e7dbaa4"), "id" : 101, "Name" : "Chris" } { "_id" : ObjectId("5e480d7d1627c0c63e7dbaa5"), "id" : 102, "Name" : "Bob" } { "_id" : ObjectId("5e480d841627c0c63e7dbaa6"), "id" : 103, ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
379 Views
To get the count and top users, use $group along with aggregate() . Let us create a collection with documents −> db.demo264.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47ed441627c0c63e7dba9e") } > db.demo264.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47ed471627c0c63e7dba9f") } > db.demo264.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47ed491627c0c63e7dbaa0") } > db.demo264.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47ed4c1627c0c63e7dbaa1") } > db.demo264.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47ed4e1627c0c63e7dbaa2") } > db.demo264.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47ed531627c0c63e7dbaa3") }Display all documents from a collection with the help ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
173 Views
To skip documents in MongoDB, use skip(). Let us create a collection with documents −> db.demo263.insertOne({_id:100}); { "acknowledged" : true, "insertedId" : 100 } > db.demo263.insertOne({_id:200}); { "acknowledged" : true, "insertedId" : 200 } > db.demo263.insertOne({_id:300}); { "acknowledged" : true, "insertedId" : 300 }Display all documents from a collection with the help of find() method −> db.demo263.find();This will produce the following output −{ "_id" : 100 } { "_id" : 200 } { "_id" : 300 }Following is the query to skip document −> result = db.demo263.aggregate([ ... { ... $project: { ... v_id: { $ifNull: [null, [100, 200]] } ... ... } ... }, ... { $unwind: '$v_id' }, ... { $sort: { v_id: 1, _id: 1 } }, ... ... { $skip: 2 }, ... { $limit: 2 } ...]);This will produce the following output −{ "_id" : 300, "v_id" : 100 } { "_id" : 100, "v_id" : 200 }
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
194 Views
To invert result i.e. opposite of $and operation, use $OR along with $ne. Let us first create a collection with documents −> db.demo4.insert({uid:1, "Name":"Chris", "Age":22}); WriteResult({ "nInserted" : 1 }) > db.demo4.insert({uid:2, "Name":"David", "Age":21}); WriteResult({ "nInserted" : 1 }) > db.demo4.insert({uid:3, "Name":"Bob", "Age":23}); WriteResult({ "nInserted" : 1 }) > db.demo4.insert({uid:1, "Name":"Carol", "Age":20}); WriteResult({ "nInserted" : 1 })Following is the query to display all documents from a collection with the help of find() method −> db.demo4.find();This will produce the following output −{ "_id" : ObjectId("5e0a1da125ddae1f53b62221"), "uid" : 1, "Name" : "Chris", "Age" : 22 } { "_id" : ObjectId("5e0a1db025ddae1f53b62222"), "uid" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
247 Views
To modify a sequence, use findAndModify(). Let us create a collection with documents −> db.demo261.insertOne({_id:100, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 100 }Display all documents from a collection with the help of find() method −> db.demo261.find();This will produce the following output −{ "_id" : 100, "Name" : "Chris" }Following is the query to modify sequence −> db.demo262.insert({_id:"newId", sequence_value:0}) WriteResult({ "nInserted" : 1 }) > function getNext(sName){ ... ... var d= db.demo262.findAndModify({ ... query:{_id: sName}, ... update: {$inc:{sequence_value:1}}, ... new:true ... }); ... return d.sequence_value; ...}Following is the query to call ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
179 Views
To add only a single value to array, use $push. Let us first create a collection with documents −> db.demo3.insertOne( ... { ... "Information" : { ... "Of" : { ... "StudentCode" : "STUDENT101", ... "StudentFavouriteSubject" : [ ... "MySQL", ... "Java" ... ] ... } ... } ... } ... ); ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
449 Views
For this, you can use $and along with dot(.) notation. Let us first create a collection with documents −>db.demo2.insertOne({"StudentInformation":[{"StudentName":"John", "StudentAge":21}, {"StudentName":"Mike", "StudentAge":22}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e08b56e25ddae1f53b62219") } >db.demo2.insertOne({"StudentInformation":[{"StudentName":"Carol", "StudentAge":19}, {"StudentName":"Bob", "StudentAge":18}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e08b58625ddae1f53b6221a") }Following is the query to display all documents from a collection with the help of find() method −> db.demo2.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e08b56e25ddae1f53b62219"), "StudentInformation" : [ { "StudentName" : "John", "StudentAge" : 21 }, ... Read More