내 접근 방식은 예상 결과에 더 가깝습니다.
내 샘플 데이터
"_id" : 1,
"pages" : [
{
"_id" : "a",
"comment" : "Have a Nice Day"
},
{
"_id" : "b",
"comment" : "Everyday is Beautiful"
},
{
"_id" : "c",
"comment" : "All is Well"
},
{
"_id" : "d",
"comment" : "Nature is wonderful"
},
{
"_id" : "e",
"comment" : "Enjoy the moment"
}
]
db.collection.aggregate를 실행 한 후 ([{$의 언 와인드 : "$ 페이지"}])
"_id" : 1, "pages" : { "_id" : "a", "comment" : "Have a Nice Day" } }
"_id" : 1, "pages" : { "_id" : "b", "comment" : "Everyday is Beautiful" } }
"_id" : 1, "pages" : { "_id" : "c", "comment" : "All is Well" } }
"_id" : 1, "pages" : { "_id" : "d", "comment" : "Nature is wonderful" } }
"_id" : 1, "pages" : { "_id" : "e", "comment" : "Enjoy the moment" } }
는 파이프 라인에 $ 그룹을 추가
db.collectiom.aggregate ([{$ unwind : "$ pages"}, {$ group : {_ id : "$ pages"}}]);
"_id" : { "_id" : "e", "comment" : "Enjoy the moment" } }
"_id" : { "_id" : "d", "comment" : "Nature is wonderful" } }
"_id" : { "_id" : "c", "comment" : "All is Well" } }
"_id" : { "_id" : "b", "comment" : "Everyday is Beautiful" } }
"_id" : { "_id" : "a", "comment" : "Have a Nice Day" } }
희망 하시겠습니까?
원하는 출력으로 나열한 결과가 올바르게 포맷되지 않았습니다. 나는 그것을 성취 할 방법을 모른다. – Tiramisu
@Tiramisu 형식을 업데이트했습니다. – nikoss
'$ unwind' 후 파이프 라인에'{ "$ project": "id": "$ pages.id", "field": "$ pages.field"}}'를 추가하십시오. 배열 – styvane