Hi All,
I am trying to write a query for nested documents but am not able to get the desired output. If someone can guide me it will be very helpful.
I have 4 collections names user, docs, pages, paragraphs
user :
{_id: "1234abcd", "numofdocs": 2, "date": "02/02/2022"}
docs:
{{"_id" : "d1", "user_id": "1234abcd", "doc_name" : "a.pdf"},
{"_id" : "d2", "user_id": "1234abcd", "doc_name" : "b.pdf"}}
pages:
{
{"_id" : "d1p1", "doc_id" : "d1", " page_size" : [540,860]},
{"_id" : "d1p2", "doc_id" : "d1", " page_size" : [540,860]},
{"_id" : "d2p1", "doc_id" : "d2", " page_size" : [545,865]},
{"_id": "d2p2", "doc_id": "d2", " page_size" : [545,865]}
}
paragraphs:
{
{"_id" : "ap1", "page_id" : "d1p1", "text" : " hello a "},
{"_id" : "ap2", "page_id" : "d1p1", "text" : " hello b "},
{"_id" : "ap3", "page_id" : "d1p2", "text" : " hello c "},
{"_id" : "ap4", "page_id" : "d1p2", "text" : " hello d"},
{"_id" : "bp1", "page_id" : "d2p1", "text" : " hello e"},
{"_id" : "bp2", "page_id" : "d2p1", "text" : " hello f"},
{"_id" : "bp3", "page_id" : "d2p2", "text" : " hello g "},
{"_id" : "bp4", "page_id" : "d2p2", "text" : " hello h "}
}
and my desired output
{_id: "1234abcd", numofdocs: 2, date: "02/02/2022",
"doc": [{"doc_name": "a.pdf", "pages": [ {"page_size": [540,860] , "paragraph": [{"text":"hello a"},{"text":"hello b"}]},
{"page_size": [540,860] , "paragraph": [{"text":"hello c"},{"text":"hello d"}]}]},
{"doc_name" : "b.pdf", "pages": [ {"page_size": [545,865] , "paragraph": [{"text":"hello e"},{"text":"hello f"}]},
{"page_size": [545,865] , "paragraph": [{"text":"hello g"},{"text":"hello h"}]}]}]
}
so far i have tried with 5 stages like match, lookup, unwind, lookup, unwind, lookup, project
but not getting in desired format…i am getting 8 documents in which each paragraph text is attached with page and doc information which is repeating in every document.