MongoDB에 포함된 문서를 쿼리하려면 집계()를 사용합니다. 문서로 컬렉션을 만들자 −
> db.demo705.insertOne(
... {
... _id:101,
... "Information":
... [
... {
... "StudentName":"Chris",
... "StudentAge":21
... },
... {
... "StudentName":"David",
... "StudentAge":23
... },
... {
... "StudentName":"Bob",
... "StudentAge":20
... }
... ]
... }
... );
{ "acknowledged" : true, "insertedId" : 101 } find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo705.find();
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : 101, "Information" : [ { "StudentName" : "Chris", "StudentAge" : 21 }, { "StudentName" : "David", "StudentAge" : 23 }, { "StudentName" : "Bob", "StudentAge" : 20 } ] } 다음은 MongoDB에 포함된 문서를 쿼리하는 방법입니다 -
> db.demo705.aggregate(
... { $unwind: '$Information' },
... { $match: {'Information.StudentAge': {$gte: 21}}},
... { $project: {Information: 1}}
... ) 이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : 101, "Information" : { "StudentName" : "Chris", "StudentAge" : 21 } }
{ "_id" : 101, "Information" : { "StudentName" : "David", "StudentAge" : 23 } }