I have 1st collection of users:
{
"_id": {
"$oid": "5ed0f5bf815f000004006fa5"
},
"name": "admin",
"email": "admin@admin.com",
"password": "$2y$10$JxLj.sPQ.WLwE7cVHTIY3OYSar9B2OjxDzS1uQJwVmMug3mipHMVS",
"updated_at": {
"$date": "2020-06-02T07:41:23.052Z"
},
"created_at": {
"$date": "2020-05-29T11:45:03.136Z"
},
"book_ids": [
"5ed0f3efcb4b0000c500462b"
]
}
2nd collection books:
[
{
"_id": {
"$oid": "5ed0f3efcb4b0000c500462b"
},
"name": "book",
"guard_name": "web",
"updated_at": {
"$date": "2020-05-29T11:37:19.502Z"
},
"created_at": {
"$date": "2020-05-29T11:37:19.502Z"
},
"chapter_ids": [
"5ed0f3efcb4b0000c5004622",
"5ed0f3efcb4b0000c5004623",
"5ed0f3efcb4b0000c5004624",
"5ed0f3efcb4b0000c5004625",
"5ed0f3efcb4b0000c5004626",
"5ed0f3efcb4b0000c5004627",
"5ed0f3efcb4b0000c5004628",
"5ed0f3efcb4b0000c5004629",
"5ed0f3efcb4b0000c500462a"
],
"user_ids": [
"5ed0f5bf815f000004006fa5"
]
}
]
3rd collection chapters:
[
{
"_id": {
"$oid": "5ed0f3efcb4b0000c5004622"
},
"name": "users manage",
"guard_name": "web",
"updated_at": {
"$date": "2020-05-29T11:37:19.488Z"
},
"created_at": {
"$date": "2020-05-29T11:37:19.488Z"
},
"book_ids": [
"5ed0f3efcb4b0000c500462b"
]
},
{
"_id": {
"$oid": "5ed0f3efcb4b0000c5004623"
},
"name": "role list",
"guard_name": "web",
"updated_at": {
"$date": "2020-05-29T11:37:19.491Z"
},
"created_at": {
"$date": "2020-05-29T11:37:19.491Z"
},
"book_ids": [
"5ed0f3efcb4b0000c500462b"
]
},
{
"_id": {
"$oid": "5ed0f3efcb4b0000c5004624"
},
"name": "role create",
"guard_name": "web",
"updated_at": {
"$date": "2020-05-29T11:37:19.492Z"
},
"created_at": {
"$date": "2020-05-29T11:37:19.492Z"
},
"book_ids": [
"5ed0f3efcb4b0000c500462b"
]
},
{
"_id": {
"$oid": "5ed0f3efcb4b0000c5004625"
},
"name": "role edit",
"guard_name": "web",
"updated_at": {
"$date": "2020-05-29T11:37:19.493Z"
},
"created_at": {
"$date": "2020-05-29T11:37:19.493Z"
},
"book_ids": [
"5ed0f3efcb4b0000c500462b"
]
},
{
"_id": {
"$oid": "5ed0f3efcb4b0000c5004626"
},
"name": "role delete",
"guard_name": "web",
"updated_at": {
"$date": "2020-05-29T11:37:19.494Z"
},
"created_at": {
"$date": "2020-05-29T11:37:19.494Z"
},
"book_ids": [
"5ed0f3efcb4b0000c500462b"
]
},
{
"_id": {
"$oid": "5ed0f3efcb4b0000c5004627"
},
"name": "item list",
"guard_name": "web",
"updated_at": {
"$date": "2020-05-29T11:37:19.495Z"
},
"created_at": {
"$date": "2020-05-29T11:37:19.495Z"
},
"book_ids": [
"5ed0f3efcb4b0000c500462b"
]
},
{
"_id": {
"$oid": "5ed0f3efcb4b0000c5004628"
},
"name": "item create",
"guard_name": "web",
"updated_at": {
"$date": "2020-05-29T11:37:19.497Z"
},
"created_at": {
"$date": "2020-05-29T11:37:19.497Z"
},
"book_ids": [
"5ed0f3efcb4b0000c500462b"
]
},
{
"_id": {
"$oid": "5ed0f3efcb4b0000c5004629"
},
"name": "item edit",
"guard_name": "web",
"updated_at": {
"$date": "2020-05-29T11:37:19.498Z"
},
"created_at": {
"$date": "2020-05-29T11:37:19.498Z"
},
"book_ids": [
"5ed0f3efcb4b0000c500462b"
]
},
{
"_id": {
"$oid": "5ed0f3efcb4b0000c500462a"
},
"name": "item delete",
"guard_name": "web",
"updated_at": {
"$date": "2020-05-29T11:37:19.499Z"
},
"created_at": {
"$date": "2020-05-29T11:37:19.499Z"
},
"book_ids": [
"5ed0f3efcb4b0000c500462b"
]
}
]
I want users wise books and books chapters in single collection. Can Anyone please help me on this?
After all the efforts I found the solution for this:
db.collection.aggregate([
{ "$lookup": {
"from": books,
"localField": "book_ids",
"foreignField": "_id",
"as": "books"
}},
{ "$unwind": "$books" },
{ "$lookup": {
"from": chapters,
"localField": "books.chapter_ids",
"foreignField": "_id",
"as": "books.chapters",
}},
]}
This solution works for me hope it will works for the others too.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.