- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
How to store array values in MongoDB?
Let us first create a collection with documents wherein we are storing array values −
>db.demo321.insertOne({"UserDetails":[{"UserId":101,"UserName":"Chris"},{"UserId":102,"UserName":"Mike"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e511248f8647eb59e56206a") } >db.demo321.insertOne({"UserDetails":[{"UserId":103,"UserName":"Bob"},{"UserId":104,"UserName":"Sam"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e511259f8647eb59e56206b") }
Display all documents from a collection with the help of find() method −
> db.demo321.find();
This will produce the following output −
{ "_id" : ObjectId("5e511248f8647eb59e56206a"), "UserDetails" : [ { "UserId" : 101, "UserName" : "Chris" }, { "UserId" : 102, "UserName" : "Mike" } ] } { "_id" : ObjectId("5e511259f8647eb59e56206b"), "UserDetails" : [ { "UserId" : 103, "UserName" : "Bob" }, { "UserId" : 104, "UserName" : "Sam" } ] }
Advertisements