How to store array values in MongoDB?

MongoDBDatabaseBig Data Analytics

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" } ] }
raja
Published on 02-Apr-2020 09:47:50
Advertisements