Hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -
> db.demo411.insertOne(
... {
... "Information" : [
... {
... "Name1" : "Chris",
... "Name2" : "David"
... },
... {
... "Name1" : "John",
... "Name2" : "John"
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e70f19715dc524f70227682")
} Hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find () -
> db.demo411.find();
Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5e70f19715dc524f70227682"), "Information" : [ { "Name1" : "Chris", "Name2" : "David" }, { "Name1" : "John", "Name2" : "John" } ] } Sau đây là truy vấn để chỉ nhận các giá trị trong mảng -
> db.demo411.aggregate(
... [
... {$project : {
... _id : 0,
... Information : {$map : {input : "$Information", as : "out", in : ["$$out.Name1", "$$out.Name2"]}}
... }
... }
... ]
... ) Điều này sẽ tạo ra kết quả sau -
{ "Information" : [ [ "Chris", "David" ], [ "John", "John" ] ] }