
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Query MongoDB Collection Starting With
For MongoDB collection beginning with_, following is the syntax −
db.createCollection(‘_yourCollectionName’);
Insert query using below syntax −
db.getCollection('_yourCollectionName').insertOne({"yourFieldName1":"yourValue1","yourFieldName2":yourValue2,............N});
Let us first create a collection with documents −
> db.createCollection('_testUnderscoreCollectionDemo'); { "ok" : 1 } >db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"John","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb4a6140b992277dae0e4") } >db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"Carol","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5ccfb4af140b992277dae0e5") }
Following is the query to display all documents from a collection with the help of find() method −
> db.getCollection('_testUnderscoreCollectionDemo').find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ccfb4a6140b992277dae0e4"), "StudentFirstName" : "John", "StudentAge" : 23 } { "_id" : ObjectId("5ccfb4af140b992277dae0e5"), "StudentFirstName" : "Carol", "StudentAge" : 21 }
Advertisements