객체 배열에서 _id로 찾으려면 집계를 사용하고 find()를 사용하지 마십시오. 먼저 문서로 컬렉션을 생성해 보겠습니다. −
> db.demo414.insertOne(
... {
... "_id": "110",
... "details":[
... {
... "StudentName":"John",
... "StudentMarks":56
... },
... {
... "StudentName":"Robert",
... "StudentMarks":98
... }
... ]
... }
... );
{ "acknowledged" : true, "insertedId" : "110" } find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo414.find();
이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : "110", "details" : [ { "StudentName" : "John", "StudentMarks" : 56 }, { "StudentName" : "Robert", "StudentMarks" : 98 } ] } 다음은 객체 배열에서 _id로 찾는 쿼리입니다 -
> db.demo414.aggregate([{$unwind: "$details"}, {$match:{"details.StudentMarks" :56}}] ) 이것은 다음과 같은 출력을 생성합니다 -
{ "_id" : "110", "details" : { "StudentName" : "John", "StudentMarks" : 56 } }