문서로 컬렉션을 만들자 −
> db.demo411.insertOne(
... {
... "Information" : [
... {
... "Name1" : "Chris",
... "Name2" : "David"
... },
... {
... "Name1" : "John",
... "Name2" : "John"
... }
... ]
... }
... );
{
"acknowledged" : true,
"insertedId" : ObjectId("5e70f19715dc524f70227682")
} find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo411.find();
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : ObjectId("5e70f19715dc524f70227682"), "Information" : [ { "Name1" : "Chris", "Name2" : "David" }, { "Name1" : "John", "Name2" : "John" } ] } 다음은 배열의 값만 가져오는 쿼리입니다 -
> db.demo411.aggregate(
... [
... {$project : {
... _id : 0,
... Information : {$map : {input : "$Information", as : "out", in : ["$$out.Name1", "$$out.Name2"]}}
... }
... }
... ]
... ) 이것은 다음과 같은 출력을 생성합니다 -
{ "Information" : [ [ "Chris", "David" ], [ "John", "John" ] ] }