my query
db.sections.aggregate([ { "$lookup": { "from": "faqs", "localField": "_id", "foreignField": "section_id", "as": "faq","pipeline": [{ $project: { _id: 1, question: 1,answer:1 } }] } }]);
response
[
{
_id: '63e4a4d9304637ec1b578136',
name: 'vaaaaaq',
is_active: true,
created_by: 'string',
updated_by: 'string',
created_at: '2023-02-09T07:46:33.794565',
updated_at: ISODate("2023-02-09T12:21:03.136Z"),
faq: [
{
_id: '63e4cd7a1fdf5ee828bb33b9',
question: 'what is',
answer: 'it is'
},
{
_id: '63e628fc8f3fb951cddfac02',
question: 'wwww',
answer: 'ssss'
}
]
},
{
_id: '63e4c2056f1845a3fc2180c5',
name: 'new section',
is_active: true,
created_by: null,
updated_by: null,
created_at: '2023-02-09T09:51:01.877699',
updated_at: '2023-02-09T09:51:01.877702',
faq: [
{
_id: '63e7bfc9865085b29e65a97d',
question: 'it is new ques',
answer: 'nothonv'
}
]
}
]
above is my mongoshell data which is fetching me two objects.
in python
sections_data = {
"$lookup": {
"from": "faqs",
"localField": "_id",
"foreignField": "section_id",
"as": "data",
"pipeline":[
{
"$project":{
"_id": 1,
"question": 1,
"answer":1
}
}
]
}
}
pipeline=[
sections_data,
]
async def get_list():
results = db['sections'].aggregate(pipeline)
results = await results.to_list(1000)
for r in results:
return r
this is my code where it is returning only one set of data response
{
"_id": "63e4a4d9304637ec1b578136",
"name": "vaaaaaq",
"is_active": true,
"created_by": "string",
"updated_by": "string",
"created_at": "2023-02-09T07:46:33.794565",
"updated_at": "2023-02-09T12:21:03.136000",
"data": [
{
"_id": "63e4cd7a1fdf5ee828bb33b9",
"question": "what is",
"answer": "it is"
},
{
"_id": "63e628fc8f3fb951cddfac02",
"question": "wwww",
"answer": "ssss"
}
]
}
in section_data if i enclose within array like ,
sections_data=[{}]
this error is coming where i am missing and how to resolve this. thanks for inputs.
results = await db['sections'].aggregate(pipeline).to_list(1000)