
- 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 MongoDB result in an array?
To store MongoDB result in an array, use the toArray() method −
var anyVariableName=db.yourCollectionName.find().toArray();
Let us first create a collection with documents −
> db.mongoDbResultInArrayDemo.insertOne({"CustomerName":"David Miller","CustomerAge":24,"isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99bd5b50a6c6dd317ad92") } > db.mongoDbResultInArrayDemo.insertOne({"CustomerName":"Sam Williams","CustomerAge":46,"isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99beab50a6c6dd317ad93") } > db.mongoDbResultInArrayDemo.insertOne({"CustomerName":"Carol Taylor","CustomerAge":23,"isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5cd99bf9b50a6c6dd317ad94") }
Following is the query to display all documents from a collection with the help of find() method −
> db.mongoDbResultInArrayDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd99bd5b50a6c6dd317ad92"), "CustomerName" : "David Miller", "CustomerAge" : 24, "isMarried" : false } { "_id" : ObjectId("5cd99beab50a6c6dd317ad93"), "CustomerName" : "Sam Williams", "CustomerAge" : 46, "isMarried" : true } { "_id" : ObjectId("5cd99bf9b50a6c6dd317ad94"), "CustomerName" : "Carol Taylor", "CustomerAge" : 23, "isMarried" : false }
Following is the query to store MongoDB result in an array −
> var mongoDbResultIntoArray=db.mongoDbResultInArrayDemo.find().toArray();
Let us display the records of above variable −
> mongoDbResultIntoArray
This will produce the following output −
[ { "_id" : ObjectId("5cd99bd5b50a6c6dd317ad92"), "CustomerName" : "David Miller", "CustomerAge" : 24, "isMarried" : false }, { "_id" : ObjectId("5cd99beab50a6c6dd317ad93"), "CustomerName" : "Sam Williams", "CustomerAge" : 46, "isMarried" : true }, { "_id" : ObjectId("5cd99bf9b50a6c6dd317ad94"), "CustomerName" : "Carol Taylor", "CustomerAge" : 23, "isMarried" : false } ]
- Related Articles
- How to store array values in MongoDB?
- Projection result as an array of selected items in MongoDB?
- MongoDB projection result as an array of selected items?
- How to store Query Result in a variable using MySQL?
- How to push an array in MongoDB?
- How to subtract elements of two arrays and store the result as a positive array in JavaScript?
- How to store query output in temp MongoDB database?
- How to store query result (a single document) into a variable?
- How to convert/store a string of numbers in to an array in java?
- Test Numpy array values for infiniteness and store the result in a new location
- How to delete element from an array in MongoDB?
- How to push an element into array in MongoDB?
- Test array values for finiteness and store the result in a new location in Numpy
- Test array values for NaT and store the result in a new location in Numpy
- Test array values for NaN and store the result in a new location in Numpy

Advertisements