- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Is it possible to use variable for collection name using PyMongo?
PyMongo is a Python distribution containing tools for working with MongoDB. Use the following syntax to use a variable for collection name −
var yourVariableName="yourCollectionName"; db[storeCollectionName].yourOperationName;
To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows. We have used variable for collection name −
> var storeCollectionName="new_Collection"; > db[storeCollectionName].insertOne({"UserName":"John","UserAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c912aea4afe5c1d2279d6a0") } > db[storeCollectionName].insertOne({"UserName":"Carol","UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c912af54afe5c1d2279d6a1") } > db[storeCollectionName].insertOne({"UserName":"Mike","UserAge":27}); { "acknowledged" : true, "insertedId" : ObjectId("5c912afe4afe5c1d2279d6a2") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db[storeCollectionName].find().pretty();
The following is the output −
{ "_id" : ObjectId("5c912aea4afe5c1d2279d6a0"), "UserName" : "John", "UserAge" : 21 } { "_id" : ObjectId("5c912af54afe5c1d2279d6a1"), "UserName" : "Carol", "UserAge" : 24 } { "_id" : ObjectId("5c912afe4afe5c1d2279d6a2"), "UserName" : "Mike", "UserAge" : 27 }
- Related Articles
- Is it possible to use MongoDB capped collection?
- Is it possible to use pyplot without DISPLAY?
- Variable collection name in MongoDB shell with JavaScript?
- Is it possible to use Python modules in Octave?
- Is it possible to use JSF+Facelets with HTML 4/5?
- Is it possible to use $(this) and universal selector (*) with jQuery?
- Is it possible to use UPDATE query with LIMIT in MySQL?
- Is it possible to plot implicit equations using Matplotlib?
- Is it possible to create a class without a name in Java?
- Is it possible to use MongoDB field value as pattern in $regex?
- Is it possible to use this keyword in static context in java?
- Can I use a JavaScript variable before it is declared?
- Is it possible to write data to file using only JavaScript?
- While using the ROLLUP modifier, is it possible to use a MySQL ORDER BY clause to sort the result?
- Is it possible for human beings to live without any goals?

Advertisements