- 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
MongoDB: How to query a collection named “version”?
For this, use the concept of createCollection() and getCollection(). Following is the query to create a collection with name ‘version’ −
> db.createCollection('version'); { "ok" : 1 }
Let us first create a collection with documents −
> db.getCollection('version').insertOne({"VersionName":"1.0"}); { "acknowledged" : true, "insertedId" : ObjectId("5e100b47d7df943a7cec4fae") } > db.getCollection('version').insertOne({"VersionName":"1.1"}); { "acknowledged" : true, "insertedId" : ObjectId("5e100b49d7df943a7cec4faf") } > db.getCollection('version').insertOne({"VersionName":"1.2"}); { "acknowledged" : true, "insertedId" : ObjectId("5e100b4cd7df943a7cec4fb0") }
Following is the query to display all documents from a collection with the help of find() method −
> db.getCollection('version').find();
This will produce the following output −
{ "_id" : ObjectId("5e100b47d7df943a7cec4fae"), "VersionName" : "1.0" } { "_id" : ObjectId("5e100b49d7df943a7cec4faf"), "VersionName" : "1.1" } { "_id" : ObjectId("5e100b4cd7df943a7cec4fb0"), "VersionName" : "1.2" }
Advertisements