如何将数组值存储在 MongoDB 中?
让我们首先创建一个包含我们存储数组值的文档的集合 −
>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") }
使用 find() 方法从集合中显示所有文档 −
> db.demo321.find();
这将生成以下输出 −
{ "_id" : ObjectId("5e511248f8647eb59e56206a"), "UserDetails" : [ { "UserId" : 101, "UserName" : "Chris" }, { "UserId" : 102, "UserName" : "Mike" } ] } { "_id" : ObjectId("5e511259f8647eb59e56206b"), "UserDetails" : [ { "UserId" : 103, "UserName" : "Bob" }, { "UserId" : 104, "UserName" : "Sam" } ] }
广告