首先让我们创建一个包含文档的集合,其中存储数组值-
>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" } ] }