
- 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
How to store query result (a single document) into a variable?
To store query result (a single document) into a variable, you can use var. Following is the syntax
var anyVariableName=db.yourCollectionName.find().limit(1); yourVariableName; //Print the records;
Let us first create a collection with documents
> db.storeQueryResultDemo.insertOne({"ClientName":"Chris","ClientAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ecf8fd628fa4220163b8d") } > db.storeQueryResultDemo.insertOne({"ClientName":"Robert","ClientAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ecf97d628fa4220163b8e") } > db.storeQueryResultDemo.insertOne({"ClientName":"Sam","ClientAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ecf9ed628fa4220163b8f") } > db.storeQueryResultDemo.insertOne({"ClientName":"Mike","ClientAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c9ecfa8d628fa4220163b90") }
Following is the query to display all documents from a collection with the help of find() method
> db.storeQueryResultDemo.find().pretty();
This will produce the following output
{ "_id" : ObjectId("5c9ecf8fd628fa4220163b8d"), "ClientName" : "Chris", "ClientAge" : 23 } { "_id" : ObjectId("5c9ecf97d628fa4220163b8e"), "ClientName" : "Robert", "ClientAge" : 21 } { "_id" : ObjectId("5c9ecf9ed628fa4220163b8f"), "ClientName" : "Sam", "ClientAge" : 25 } { "_id" : ObjectId("5c9ecfa8d628fa4220163b90"), "ClientName" : "Mike", "ClientAge" : 26 }
Following is the query to store query result (a single document) into a variable
> var queryResult=db.storeQueryResultDemo.find().limit(1);
Following is the query to display records of the above variable
> queryResult;
This will produce the following output
{ "_id" : ObjectId("5c9ecf8fd628fa4220163b8d"), "ClientName" : "Chris", "ClientAge" : 23 }
- Related Articles
- How to store Query Result in a variable using MySQL?
- How to assign the result of a MySQL query into a variable?
- Store a variable with the result of a MySQL SELECT CASE?
- MongoDB findById returning a list of documents instead of a single result? How to get only a single document?
- Set the result of a query to a variable in MySQL?
- How to insert into two tables using a single MySQL query?
- Store a column's value into a MySQL stored procedure’s variable
- How to collapse rows into a comma-delimited list with a single MySQL Query?
- MongoDB query to set user defined variable into query?
- MongoDB query to push document into an array
- Convert JavaScript array iteration result into a single line text string
- Set user variable from result of query in MySQL?
- How to use custom variable while updating a MongoDB document?
- MongoDB findOneAndUpdate() to update a single document
- How to store MongoDB result in an array?

Advertisements