![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
236 Views
You can use projection operator $elemMatch to filter in queried element in an object array in MongoDB collection. To retrieve only the queried element in an object array in MongoDB, let us first create a collection with documents object array.The query is as follows:> db.objectArray.insert({"Persons":[ {"PersonName":"Adam", "PersonSalary":25000}, {"PersonName":"Larry", "PersonSalary":27000 }]}); WriteResult({ "nInserted" : 1 }) > db.objectArray.insert({"Persons":[ {"PersonName":"David", "PersonSalary":32000}, {"PersonName":"Carol", "PersonSalary":77000 }]}); WriteResult({ "nInserted" : 1 })Now you can display all the documents with the help of find(). The query is as follows:> db.objectArray.find().pretty();The following is the output:{ "_id" : ObjectId("5c6bfadc68174aae23f5ef53"), "Persons" ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
1K+ Views
To retrieve documents from a collection in MongoDB, you need to use find() method. The syntax is as follows:db.yourCollectionName.find();The above syntax will return all the documents from a collection in MongoDB. To understand the above syntax, let us create a collection with documents. The query to create documents are as follows:> db.retrieveAllStudents.insertOne({"StudentId":"STUD101", "StudentName":"David", "StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c6bf5cf68174aae23f5ef4e") } > db.retrieveAllStudents.insertOne({"StudentId":"STUD102", "StudentName":"Carol", "StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c6bf5e968174aae23f5ef4f") } > db.retrieveAllStudents.insertOne({"StudentId":"STUD103", "StudentName":"Maxwell", "StudentAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c6bf5f768174aae23f5ef50") } > db.retrieveAllStudents.insertOne({"StudentId":"STUD104", "StudentName":"Bob", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ... Read More
![Krantik Chavan](https://www.tutorialspoint.com/assets/profiles/13545/profile/60_126883-1512724834.jpg)
555 Views
If you want to delete all documents from the collection, you can use deleteMany(). Let us first create a collection and insert some documents to it:> db.deleteDocumentsDemo.insert({"Name":"Larry", "Age":23}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Mike", "Age":21}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Sam", "Age":24}); WriteResult({ "nInserted" : 1 })Now display all the documents from the collection. The query is as follows:> db.deleteDocumentsDemo.find().pretty();The following is the output:{ "_id" : ObjectId("5c6ab0e064f3d70fcc914805"), "Name" : "Larry", "Age" : 23 } { "_id" : ObjectId("5c6ab0ef64f3d70fcc914806"), "Name" : "Mike", "Age" : 21 } { "_id" : ObjectId("5c6ab0f864f3d70fcc914807"), "Name" ... Read More
![Daniol Thomas](https://www.tutorialspoint.com/assets/profiles/13551/profile/60_130035-1512726078.jpg)
236 Views
To delete document from a collection in MongoDB, you can use the deleteOne() method. Let us first create a collection and insert some documents to it:> db.deleteDocumentsDemo.insert({"Name":"Larry", "Age":23}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Mike", "Age":21}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Sam", "Age":24}); WriteResult({ "nInserted" : 1 })Now display all the documents from the collection. The query is as follows:> db.deleteDocumentsDemo.find().pretty();The following is the output:{ "_id" : ObjectId("5c6ab0e064f3d70fcc914805"), "Name" : "Larry", "Age" : 23 } { "_id" : ObjectId("5c6ab0ef64f3d70fcc914806"), "Name" : "Mike", "Age" : 21 } { "_id" : ObjectId("5c6ab0f864f3d70fcc914807"), "Name" : "Sam", ... Read More
![Daniol Thomas](https://www.tutorialspoint.com/assets/profiles/13551/profile/60_130035-1512726078.jpg)
338 Views
To delete the documents from a collection in MongoDB, you need to use remove() method. The syntax is as follows:db.yourCollectionName.remove(yourDeleteValue);Here, let us create a collection with some documents. The query is as follows:>db.deleteDocuments.insert({"UserId":1, "UserName":"Bob", "UserTechnicalSubject":"Introducti on to PL/SQL"}); WriteResult({ "nInserted" : 1 }) >db.deleteDocuments.insert({"UserId":2, "UserName":"Carol", "UserTechnicalSubject":"Introduction to MongoDB"}); WriteResult({ "nInserted" : 1 }) >db.deleteDocuments.insert({"UserId":3, "UserName":"John", "UserTechnicalSubject":"Introduction to MySQL"}); WriteResult({ "nInserted" : 1 }) >db.deleteDocuments.insert({"UserId":4, "UserName":"Maxwell", "UserTechnicalSubject":"Introduction to SQL Server"}); WriteResult({ "nInserted" : 1 })You can display all documents from the collection we created above with the help of find() command. The query is as follows:> db.deleteDocuments.find().pretty();The following ... Read More
![Daniol Thomas](https://www.tutorialspoint.com/assets/profiles/13551/profile/60_130035-1512726078.jpg)
232 Views
To insert new documents into a MongoDB collection, you need to use insert() method or save() method.Case 1: Using insert() method.The syntax is as follows:db.yourCollectionName.insert(yourDocument);Case 2: Using save() method.The syntax is as follows:db.yourCollectionName.save(yourDocument);In the above syntax, if your collection name does not exist then MongoDB will create a new collection and insert the document in the collection.The query to insert new documents is as follows for both the cases discussed above.Case 1: Using insert() method. The query is as follows:> db.userInformation.insert({ ... "Name":"John", ... Age:30, ... isStudent:false, ... "Subjects":["Introduction to java", "Introduction to MongoDB"] ... ... Read More
![Alex Onsman](https://www.tutorialspoint.com/assets/profiles/13357/profile/60_91536-1512482971.jpg)
1K+ Views
Open source databases are those databases who have an open source code i.e anyone may view the code, study it or even modify it. Open source databases could be relational (SQL) or non relational (NoSQL).Why use Open Source Databases?It is quite expensive to create and maintain a database for any company. A huge chunk of the total software expenditure is used to handle databases. So, it is feasible to switch to low cost open source databases. This saves companies a lot of money in the long run.Open Source Databases in useThere many different open source databases in the market. All ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
566 Views
You can use the pymongo library in Python to connect to a MongoDB database and use it to insert, update, delete, etc objects in Python. The library supports Python datetime objects out of the box and you dont need to do anything special to insert dates in Mongo using PyMongo. For example, Examplefrom pymongo import MongoClient # This will try to connect to MongoDB on the default port and host client = MongoClient() db = client.test_database # Insert the given dictionary to the objects collection: result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Object inserted!")OutputThis will give the output −Object inserted!Note − Always use ... Read More
![Rajendra Dharmkar](https://www.tutorialspoint.com/assets/profiles/9963/profile/60_124173-1512724240.jpg)
1K+ Views
You can use the pymongo library in Python to connect to a MongoDB database and use it to insert, update, delete, etc objects in Python. The library supports Python datetime objects out of the box and you dont need to do anything special to insert dates in Mongo using PyMongo.Examplefrom pymongo import MongoClient # This will try to connect to MongoDB on the default port and host client = MongoClient() db = client.test_database # Insert the given dictionary to the objects collection: result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Object inserted!")OutputThis will give the output −Object inserted!Note − Always use datetime.datetime.utcnow(), which returns ... Read More