
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
MongoDB query to set user defined variable into query?
For user-defined variables, use var keyword in MongoDB. Let us create a collection with documents −
> db.demo327.insertOne({"FirstName":"Chris","LastName":"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e516952f8647eb59e562076") } > db.demo327.insertOne({"FirstName":"David","LastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e51695af8647eb59e562077") } > db.demo327.insertOne({"FirstName":"John","LastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e516962f8647eb59e562078") } > db.demo327.insertOne({"FirstName":"John","LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e516968f8647eb59e562079") }
Display all documents from a collection with the help of find() method −
> db.demo327.find();
This will produce the following output −
{ "_id" : ObjectId("5e516952f8647eb59e562076"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5e51695af8647eb59e562077"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ObjectId("5e516962f8647eb59e562078"), "FirstName" : "John", "LastName" : "Doe" } { "_id" : ObjectId("5e516968f8647eb59e562079"), "FirstName" : "John", "LastName" : "Smith" }
Following is how to set a user-defined variable into the query −
> var name="John"; > db.demo327.find({"FirstName":name});
This will produce the following output −
{ "_id" : ObjectId("5e516962f8647eb59e562078"), "FirstName" : "John", "LastName" : "Doe" } { "_id" : ObjectId("5e516968f8647eb59e562079"), "FirstName" : "John", "LastName" : "Smith" }
- Related Articles
- Push query results into variable with MongoDB?
- Set user variable from result of query in MySQL?
- Perform MySQL SELECT INTO user-defined variable
- Select into a user-defined variable with MySQL
- Add user defined value to a column in a MySQL query?
- Set user-defined variable with table name in MySQL prepare statement?
- MongoDB query to push document into an array
- MongoDB query to increment a specific value using custom variable
- A single MongoDB query to orderby date and group by user
- How to set different auto-increment ids for two tables with a user-defined variable?
- MongoDB query to change simple field into an object?
- MySQL ORDER BY with numeric user-defined variable?
- How to store query result (a single document) into a variable?
- Set the result of a query to a variable in MySQL?
- MongoDB query on nth element (variable index) of subdocument array

Advertisements