- 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
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
64 Views
Yes, to display a database in the list, first create a database and add collection(s), else it won’t be visible in the list. After that, use the SHOW dbs command to display the database name in the list of databases.Following is the query to create a database −> use webcustomertracker; switched to db webcustomertrackerLet us first create a collection with documents −> db.first_Collection.insert({"Name":"Chris"}); WriteResult({ "nInserted" : 1 })Following is the query to display all documents from a collection with the help of find() method −> db.first_Collection.find();This will produce the following output −{ "_id" : ObjectId("5ce2760836e8b255a5eee94a"), "Name" : "Chris" }Following is ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
248 Views
You can’t directly update _id field i.e. write some script to update. Let us first create a collection with documents −> db.updatingIdFieldDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5ce271bb36e8b255a5eee949") }Following is the query to display all documents from a collection with the help of find() method −> db.updatingIdFieldDemo.find();This will produce the following output −{ "_id" : ObjectId("5ce271bb36e8b255a5eee949"), "StudentName" : "Chris" }Following is the query to update _id field in MongoDB −> var myDocument=db.updatingIdFieldDemo.findOne({StudentName:"Chris"}); > myDocument._id = 101; 101 > db.updatingIdFieldDemo.save(myDocument); WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 101 }) > db.updatingIdFieldDemo.remove({_id:ObjectId("5ce271bb36e8b255a5eee949")}); WriteResult({ ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
71 Views
To reduce the time to find record in MongoDB, you can use index. Following is the syntax −db.yourCollectionName.createIndex({yourFieldName:1});You can follow the below approaches to create index for field names based on number, text, hash, etc.First ApproachLet us create an index. Following is the query −> db.takeLessTimeToSearchDemo.createIndex({"EmployeeName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Second ApproachTo understand the above concept, let us create another index −> db.takeLessTimeToSearchDemo1.createIndex({"EmployeeName":"text"}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Third ApproachLet us now create another index −> db.takeLessTimeToSearchDemo2.createIndex({"EmployeeName":"hashed"}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
387 Views
Use aggregate framework along with $literal operator. Let us first create a collection with documents −> db.replaceValueDemo.insertOne( { _id : 100, "EmployeeName" :"Chris", "EmployeeOtherDetails": { "EmployeeDesignation" : "HR", "EmployeeAge":27 } } ); { "acknowledged" : true, "insertedId" : 100 } > db.replaceValueDemo.insertOne( { _id : 101, "EmployeeName" :"David", "EmployeeOtherDetails": { "EmployeeDesignation" : "Tester", "EmployeeAge":26 } } ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
794 Views
You can use $in operator for this. Let us first create a collection with documents −> db.searchForStringOrNumberDemo.insertOne( { "_id": new ObjectId(), "StudentName": "Larry", "StudentDetails": { "StudentMarks": { "StudentMongoDBMarks": [44] } } } ); { "acknowledged" : true, "insertedId" : ObjectId("5ce2407c36e8b255a5eee944") } > db.searchForStringOrNumberDemo.insertOne( { "_id": new ObjectId(), "StudentName": "Larry", "StudentDetails": { "StudentMarks": { "StudentMongoDBMarks": ["44"] } } } ); { "acknowledged" : true, "insertedId" : ObjectId("5ce240f036e8b255a5eee945") }Following is ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
583 Views
To return specific fields, use aggregate $project. Let us first create a collection with documents −> db.returnSpecificFieldDemo.insertOne( { "StudentId":1, "StudentDetails": [ { "StudentName":"Larry", "StudentAge":21, "StudentCountryName":"US" }, { "StudentName":"Chris", "StudentAge":23, "StudentCountryName":"AUS" } ] } ); { "acknowledged" ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
145 Views
For this, use the aggregate framework. Let us first create a collection with documents −> db.aggregateArrayDemo.insertOne( { "_id":100, "UserDetails": [ { "UserName": "John", "UserLoginYear":2010 }, { "UserName": "Carol", "UserLoginYear":2019 } ] } ); { "acknowledged" : true, "insertedId" : 100 }Following is the query to display all documents from a collection with the help of find() method −> db.aggregateArrayDemo.find().pretty();This will produce the following output −{ "_id" : 100, "UserDetails" : [ { "UserName" : "John", "UserLoginYear" : 2010 }, { "UserName" : "Carol", "UserLoginYear" : 2019 } ] }Following is the query to aggregate array documents −> db.aggregateArrayDemo.aggregate([ { $match: { _id: 100 } }, { $project: { Minimum: { $min: "$UserDetails.UserLoginYear" }, Maximum: { $max: "$UserDetails.UserLoginYear" } } } ]);This will produce the following output −{ "_id" : 100, "Minimum" : 2010, "Maximum" : 2019 }
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
102 Views
To get the matching document, use $elemMatch. Let us first create a collection with documents −> db.getMatchingDocumentDemo.insertOne( { _id :1, "UserDetails":[ { "UserName":"John", "UserAge":23 } ] } ); { "acknowledged" : true, "insertedId" : 1 } > db.getMatchingDocumentDemo.insertOne( { _id :2, "UserDetails":[ { "UserName":"Larry", "UserAge":24 } ] } ); { "acknowledged" : true, "insertedId" : 2 }Following is the query to display all documents from a collection with the ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
445 Views
You can use update(). Let us first create a collection with documents −> db.addExtraFieldDemo.insertOne( { "_id": 1, "UserName": "Larry" , "UserOtherDetails":[ { "UserCountryName": "US", "UserAge":23 }, { "UserCountryName": "UK", "UserAge":24 } ] } ); { "acknowledged" : true, "insertedId" : 1 }Following is the query to ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
477 Views
Let us first create a collection with documents −> db.deleteMultipleDocumentsDemo.insertOne({"StudentFirstName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5ce00b07bf3115999ed51214") } > db.deleteMultipleDocumentsDemo.insertOne({"StudentFirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5ce00b0bbf3115999ed51215") } > db.deleteMultipleDocumentsDemo.insertOne({"StudentFirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5ce00b0fbf3115999ed51216") } > db.deleteMultipleDocumentsDemo.insertOne({"StudentFirstName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5ce00b12bf3115999ed51217") } > db.deleteMultipleDocumentsDemo.insertOne({"StudentFirstName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5ce00b18bf3115999ed51218") }Following is the query to display all documents from a collection with the help of find() method −> db.deleteMultipleDocumentsDemo.find();This will produce the following output −{ "_id" : ObjectId("5ce00b07bf3115999ed51214"), "StudentFirstName" : "Larry" } { ... Read More