- 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)
171 Views
For bulk insert, you can use insert() in MongoDB. Let us create a collection with documents −> var manyDocument = db.demo255.initializeUnorderedBulkOp(); > manyDocument.insert( { "Name":"Chris", Age:24} ); > manyDocument.insert( {"Name":"Bob", Age:22 } ); > manyDocument.insert( { "Name":"David", Age:23 } ); > manyDocument.execute(); BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 3, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })Display all documents from a collection with the help of find() method −> db.demo255.find();This will produce the following output −{ "_id" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
314 Views
For this, use mapReduce(). Let us first create a collection with documents −> db.splitString.insertOne({"StudentName":"John Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e0849d925ddae1f53b62206") }Following is the query to display all documents from a collection with the help of find() method −> db.splitString.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e0849d925ddae1f53b62206"), "StudentName" : "John Smith" }Here is the query to split a string −> db.splitString.mapReduce( ... function() { ... var StudentLastName = this.StudentName.split(/\s/).reverse()[0].toUpperCase(); ... ... emit({ "StudentLastName": StudentLastName, "FirstObjectId": this._id }, this); ... }, ... function(){}, ... ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
181 Views
Fir this, you can use $rename. Let us first create a collection with documents −> db.changeSimpleFieldDemo.insertOne({"StudentMarks":58, "StudentSubject":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e0847a825ddae1f53b62205") }Following is the query to display all documents from a collection with the help of find() method −> db.changeSimpleFieldDemo.find();This will produce the following output −{ "_id" : ObjectId("5e0847a825ddae1f53b62205"), "StudentMarks" : 58, "StudentSubject" : "MySQL" }Here is the query to change a field into an object. The “obj” field is a temporary field name we have used below −> db.changeSimpleFieldDemo.update({}, {$rename: {Student: 'obj'}}, {multi: true}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
84 Views
To sort in MongoDB, use sort(). For displaying only a specific number of values, use LIMIT. Let us create a collection with documents −> db.demo254.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47a0ab1627c0c63e7dba7f") } > db.demo254.insertOne({"Name":"Adam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47a0b01627c0c63e7dba80") } > db.demo254.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e47a0b71627c0c63e7dba81") }Display all documents from a collection with the help of find() method −> db.demo254.find();This will produce the following output −{ "_id" : ObjectId("5e47a0ab1627c0c63e7dba7f"), "Name" : "Chris" } { "_id" : ObjectId("5e47a0b01627c0c63e7dba80"), "Name" : "Adam" } { "_id" : ObjectId("5e47a0b71627c0c63e7dba81"), "Name" : "Bob" ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
578 Views
To display number of databases in MongoDB, you need to create atleast one document in a database.Let’s say, you have created a database, but did not added any document in it. Then in the list of databases that particular database won’t be visible.Following is the query to create a database −> use app; switched to db appFollowing is the query to display all databases −> show dbs;This will produce the following output. The new database “app” won’t be visible since we haven’t added atleast one document in it −admin 0.002GB business ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
346 Views
To match and remove element(s) , use MongoDB $pullAll. Let us first create a collection with documents −> db.removeElementsDemo.insertOne({"ListOfNames":["Mike", "Sam", "David", "Carol"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e071e5a25ddae1f53b62203") }Following is the query to display all documents from a collection with the help of find() method −> db.removeElementsDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e071e5a25ddae1f53b62203"), "ListOfNames" : [ "Mike", "Sam", "David", "Carol" ] }Here is the query to match and remove element(s) from an array ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
11K+ Views
To find by id in MongoDB, use the find() method as in the below syntax −db.findByIdDemo.find({"_id" :yourObjectId});To understand the above syntax, let us create a collection with documents −> db.findByIdDemo.insertOne({"Value":10}); { "acknowledged" : true, "insertedId" : ObjectId("5e07158925ddae1f53b621fc") } > db.findByIdDemo.insertOne({"Value":500}); { "acknowledged" : true, "insertedId" : ObjectId("5e07158c25ddae1f53b621fd") } > db.findByIdDemo.insertOne({"Value":1000}); { "acknowledged" : true, "insertedId" : ObjectId("5e07159125ddae1f53b621fe") }Following is the query to display all documents from a collection with the help of find() method −> db.findByIdDemo.find();This will produce the following output −"_id" : ObjectId("5e07158925ddae1f53b621fc"), "Value" : 10 } { "_id" : ObjectId("5e07158c25ddae1f53b621fd"), "Value" : ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
449 Views
Use find() along with $elemMatch to fetch array values. Let us first create a collection with documents −> db.fetchingArrayValuesDemo.insertOne( ... { ... "StudentName": "David", ... "StudentDetails": [ ... { ... "FatherName": "Bob", ... "CountryName": "US", ... ... "Favourite": [ ... { ... "Teacher": "DAVID", ... "Subject": [ ... "MySQL", ... ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
359 Views
To implement indexes correctly with $elemMatch, you need to use the concept of explain(). Let us first create a collection with documents −> db.workingOfIndexesDemo.createIndex({"Information.StudentDetails.StudentName":1}, { sparse : true, background : true } ); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"Chris"}}}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f94825ddae1f53b621f7") } > db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"David"}}}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f94f25ddae1f53b621f8") } > db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"Mike"}}}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f95325ddae1f53b621f9") }Following is the query to display all documents from a collection with the help ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
198 Views
For this, you can use $cond along with $anyElementTrue. NULL values (absence of a field) would evaluate to FALSE. With that, an empty array also returns FALSE with $ anyElementTrue.Let us first create a collection with documents −> db.presenceDemo.insertOne({"StudentName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f70c25ddae1f53b621f3") } > db.presenceDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f71425ddae1f53b621f4") } > db.presenceDemo.insertOne({"StudentName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f71825ddae1f53b621f5") } > db.presenceDemo.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e06f71e25ddae1f53b621f6") }Following is the query to display all documents from a collection with the help of find() ... Read More