- 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
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
98 Views
For conditional upserts or updates, you can use $max operator. Let us first create a collection with documents>db.conditionalUpdatesDemo.insertOne({"_id":100, "StudentFirstScore":89, "StudentSecondScore":78, "BiggestScore":89}); { "acknowledged" : true, "insertedId" : 100 } >db.conditionalUpdatesDemo.insertOne({"_id":101, "StudentFirstScore":305, "StudentSecondScore":560, "BiggestScore":1050}); { "acknowledged" : true, "insertedId" : 101 } >db.conditionalUpdatesDemo.insertOne({"_id":103, "StudentFirstScore":560, "StudentSecondScore":789, "BiggestScore":880}); { "acknowledged" : true, "insertedId" : 103 } Following is the query to display all documents from a collection with the help of find() method: > db.conditionalUpdatesDemo.find().pretty();This will produce the following output{ "_id" : 100, "StudentFirstScore" : 89, "StudentSecondScore" : 78, "BiggestScore" : 89 } { "_id" : 101, ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
648 Views
Yes, you can use pull and add at the same time with $addToSet and $pull operator. Let us first create a collection with documents> db.pullAndAddToSetDemo.insertOne({StudentScores : [78, 89, 90]} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c9a797e15e86fd1496b38af") }Following is the query to display all documents from a collection with the help of find() method> db.pullAndAddToSetDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9a797e15e86fd1496b38af"), "StudentScores" : [ 78, 89, 90 ] }Following is the query to pull and addtoset at the same time in MongoDB> var ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
905 Views
To find oldest/youngest post in MongoDB collection, you can use sort(). Let’s say you have a document with a field “UserPostDate” and you need to get the oldest and youngest post. For that, Let us first create a collection with documents>db.getOldestAndYoungestPostDemo.insertOne({"UserId":"Larry@123", "UserName":"Larry", "UserPostDate":new ISODate('2019-03-27 12:00:00')}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a700f15e86fd1496b38ab") } >db.getOldestAndYoungestPostDemo.insertOne({"UserId":"Sam@897", "UserName":"Sam", "UserPostDate":new ISODate('2012-06-17 11:40:30')}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a703815e86fd1496b38ac") } >db.getOldestAndYoungestPostDemo.insertOne({"UserId":"David@777", "UserName":"David", "UserPostDate":new ISODate('2018-01-31 10:45:35')}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a705e15e86fd1496b38ad") } >db.getOldestAndYoungestPostDemo.insertOne({"UserId":"Chris@909", "UserName":"Chris", "UserPostDate":new ISODate('2017-04-14 04:12:04')}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a708915e86fd1496b38ae") }Following ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
115 Views
To increment a value inside a nested array, use positional operator ($). Let us first create a collection with documents> db.incrementInNestedArrayDemo.insertOne( ... { ... "StudentId":100, ... "ProjectDetails": ... [ ... {"ProjectId":567778888, ... "TeamSize":4 ... }, ... { ... "ProjectId":67888999, ... "TeamSize":2 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c9a6b7b15e86fd1496b38aa") }Following is the query to display all documents from a collection with the help ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
630 Views
You can use upsert i.e. whenever you insert a value and it already exist then update would be performed. If the value does not already exist then it would get inserted.Let us first create a collection with documents> db.onlyInsertIfValueIsUniqueDemo.insertOne({"StudentName":"Larry", "StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a633815e86fd1496b38a4") } > db.onlyInsertIfValueIsUniqueDemo.insertOne({"StudentName":"Mike", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a634a15e86fd1496b38a5") } > db.onlyInsertIfValueIsUniqueDemo.insertOne({"StudentName":"Sam", "StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a635015e86fd1496b38a6") }Following is the query to display all documents from a collection with the help of find() method> db.onlyInsertIfValueIsUniqueDemo.find().pretty();This will produce the following output{ "_id" ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
453 Views
Following is the syntax to count the number of documents in a MongoDB collectionlet anyVariableName= db.getCollection(‘yourCollectionName’); yourVariableName.count();Let us first create a collection with documents> db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a5e2015e86fd1496b38a1") } >db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Ramit", "CustomerAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a5e3515e86fd1496b38a2") } >db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Adam", "CustomerAge":27, "CustomerCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a5e4c15e86fd1496b38a3") }Following is the query to display all documents from a collection with the help of find() method> db.countNumberOfDocumentsDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9a5e2015e86fd1496b38a1"), "CustomerName" : "Bob" } { "_id" : ObjectId("5c9a5e3515e86fd1496b38a2"), "CustomerName" : "Ramit", "CustomerAge" ... Read More
![Ankith Reddy](https://www.tutorialspoint.com/assets/profiles/22328/profile/60_142162-1519038074.jpg)
1K+ Views
Following is the syntax to return only a single property _id in MongoDBdb.yourCollectionName.find({}, {"_id": 1}).pretty();Let us first create a collection with documents> db.singlePropertyIdDemo.insertOne({"_id":101, "UserName":"Larry", "UserAge":21}); { "acknowledged" : true, "insertedId" : 101 } > db.singlePropertyIdDemo.insertOne({"_id":102, "UserName":"Mike", "UserAge":26}); { "acknowledged" : true, "insertedId" : 102 } > db.singlePropertyIdDemo.insertOne({"_id":103, "UserName":"Chris", "UserAge":24}); { "acknowledged" : true, "insertedId" : 103 } > db.singlePropertyIdDemo.insertOne({"_id":104, "UserName":"Robert", "UserAge":23}); { "acknowledged" : true, "insertedId" : 104 } > db.singlePropertyIdDemo.insertOne({"_id":105, "UserName":"John", "UserAge":27}); { "acknowledged" : true, "insertedId" : 105 }Following is the query to display all documents from a collection with the help of find() method> db.singlePropertyIdDemo.find().pretty();This will produce ... Read More
![George John](https://www.tutorialspoint.com/assets/profiles/13511/profile/60_58410-1512537824.png)
2K+ Views
Yes, it is possible to rename using aggregation. Let us first create a collection with documents> db.renameIdDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a1760353decbc2fc927c5") } > db.renameIdDemo.insertOne({"StudentName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a1765353decbc2fc927c6") } > db.renameIdDemo.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a176b353decbc2fc927c7") }Following is the query to display all documents from a collection with the help of find() method> db.renameIdDemo.find();This will produce the following output{ "_id" : ObjectId("5c9a1760353decbc2fc927c5"), "StudentName" : "Chris" } { "_id" : ObjectId("5c9a1765353decbc2fc927c6"), "StudentName" : "Robert" } { "_id" : ObjectId("5c9a176b353decbc2fc927c7"), "StudentName" : "David" }Following is the query to ... Read More
![Chandu yadav](https://www.tutorialspoint.com/assets/profiles/22330/profile/60_142811-1519038104.jpg)
120 Views
To query MongoDB with length criteria, you can use regex. Following is the syntaxdb.yourCollectionName.find({ ‘yourFieldName’: { $regex: /^.{yourLengthValue1, yourLengthValue2}$/ } });Let us create a collection with documents. Following is the query> db.queryLengthDemo.insertOne({"StudentFullName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01ae353decbc2fc927c0") } > db.queryLengthDemo.insertOne({"StudentFullName":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01b4353decbc2fc927c1") } > db.queryLengthDemo.insertOne({"StudentFullName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01c2353decbc2fc927c2") } > db.queryLengthDemo.insertOne({"StudentFullName":"Robert Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01e2353decbc2fc927c3") } > db.queryLengthDemo.insertOne({"StudentFullName":"Chris Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9a01f1353decbc2fc927c4") }Following is the query to display ... Read More
![Arjun Thakur](https://www.tutorialspoint.com/assets/profiles/13574/profile/60_74441-1560315595.jpg)
324 Views
To iterate over all MongoDB databases, you need to switch your database to admin. Following is the query to switch to admin and get information about all the databases> switchDatabaseAdmin = db.getSiblingDB("admin"); admin > allDatabaseName = switchDatabaseAdmin.runCommand({ "listDatabases": 1 }).databases;This will produce the following output[ { "name" : "admin", "sizeOnDisk" : 495616, "empty" : false }, { "name" : "config", "sizeOnDisk" : 98304, "empty" : false }, { "name" : "local", "sizeOnDisk" : 73728, "empty" : false }, { "name" : "sample", "sizeOnDisk" : 1335296, "empty" : false }, { "name" : "sampleDemo", "sizeOnDisk" : 278528, "empty" : false }, { "name" : "studentSearch", "sizeOnDisk" : 262144, "empty" : false }, { "name" : "test", "sizeOnDisk" : 8724480, "empty" : false } ]