簡體   English   中英

在 mongo 數據庫中查找

[英]Lookup in mongo DB

我正在嘗試使用 mongoDB 為自定義博客網站創建 model 數據庫。我關注 collections

用戶收藏:

[{
  "_id": {
    "$oid": "5ed15463f43c335cb2e9ef73"
  },
  "first_name": "xxx1",
  "last_name": "xxx1",
  "email": "xxx@gmail.com"
},{
  "_id": {
    "$oid": "5ed15463f43c335cb2e9ef74"
  },
  "first_name": "xxx2",
  "last_name": "xxx2",
  "email": "xxx2@gmail.com"
},{
  "_id": {
    "$oid": "5ed15463f43c335cb2e9ef75"
  },
  "first_name": "xxx3",
  "last_name": "xxx3",
  "email": "xxx3@gmail.com"
},{
  "_id": {
    "$oid": "5ed15463f43c335cb2e9ef76"
  },
  "first_name": "xxx4",
  "last_name": "xxx4",
  "email": "xxx4@gmail.com"
},{
  "_id": {
    "$oid": "5ed15463f43c335cb2e9ef77"
  },
  "first_name": "xxx3",
  "last_name": "xxx3",
  "email": "xxx3@gmail.com"
},{
  "_id": {
    "$oid": "5ed15463f43c335cb2e9ef78"
  },
  "first_name": "xxx4",
  "last_name": "xxx4",
  "email": "xxx4@gmail.com"
}]

帖子集合:我創建了名為 Comments and likes 的嵌入式文檔,並使用了 User 集合中的 user_id 參考字段。

[{
  "_id": {
    "$oid": "5f232c18a93ce29203b3f2f6"
  },
  "post_created_user_id": {
    "$oid": "5ed15463f43c335cb2e9ef73"
  },
  "post_date": {
    "$date": "2020-07-30T20:22:48.839Z"
  },
  "post_content": "Content 1",
  "post_likes": [
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef75"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef76"
      }
    }
  ],
  "post_comments": [
    {
      "comment_id": {
        "$oid": "5f2850966efb9d4e803b2030"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
      },
      "comment_content": "Test comment1",
      "comment_date": {
        "$date": "2020-08-03T17:59:50.132Z"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850ff6efb9d4e803b2033"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef74"
      },
      "comment_content": "test comments 1",
      "comment_date": {
        "$date": "2020-08-03T18:01:35.008Z"
      }
    }
  ]
},{
  "_id": {
    "$oid": "5f232c18a93ce29203b3f2f7"
  },
  "post_created_user_id": {
    "$oid": "5ed15463f43c335cb2e9ef73"
  },
  "post_date": {
    "$date": "2020-07-30T20:22:48.839Z"
  },
  "post_content": "content 2",
  "post_likes": [
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef77"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef78"
      }
    }
  ],
  "post_comments": [
    {
      "comment_id": {
        "$oid": "5f2850966efb9d4e803b2030"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
      },
      "comment_content": "Test comment1",
      "comment_date": {
        "$date": "2020-08-03T17:59:50.132Z"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850ff6efb9d4e803b2033"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef74"
      },
      "comment_content": "test comments 1",
      "comment_date": {
        "$date": "2020-08-03T18:01:35.008Z"
      }
    }
  ]
}]

欲望 output:

 {
    "_id": {
        "$oid": "5f232c18a93ce29203b3f2f6"
    },
    "post_created_user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
    },
    "post_date": {
        "$date": "2020-07-30T20:22:48.839Z"
    },
    "post_content": "Content 1",
      "post_likes": [{
        "user_id": {
            "$oid": "5ed15463f43c335cb2e9ef73"
        },
        user_info : {
            "first_name": "xxx1",
            "last_name": "xxx1",
            "email": "xxx@gmail.com"
        
    }, {
        "user_id": {
            "$oid": "5ed15463f43c335cb2e9ef74"
        },
        user_info : {
            "first_name": "xxx2",
            "last_name": "xxx2",
            "email": "xxx2@gmail.com"
        
    }],
    "post_comments": [{
        "comment_id": {
            "$oid": "5f2850966efb9d4e803b2030"
        },
        "user_id": {
            "$oid": "5ed15463f43c335cb2e9ef73"
        },
        "comment_content": "Test comment1",
        "comment_date": {
            "$date": "2020-08-03T17:59:50.132Z"
        },
        user_info : {
            "first_name": "xxx1",
            "last_name": "xxx1",
            "email": "xxx@gmail.com"
        }
    }, {
        "comment_id": {
            "$oid": "5f2850ff6efb9d4e803b2033"
        },
        "user_id": {
            "$oid": "5ed15483f43c335cb2e9ef74"
        },
        "comment_content": "test comments 1",
        "comment_date": {
            "$date": "2020-08-03T18:01:35.008Z"
        }
        user_info : {
            "first_name": "xxx2",
            "last_name": "xxx2",
            "email": "xxx2@gmail.com"
        }
    }]
}

查找我在 Post collection 上試過的:

[
    {
        '$unwind': {
            'path': '$post_likes', 
            'preserveNullAndEmptyArrays': True
        }
    }, {
        '$unwind': {
            'path': '$post_comments', 
            'preserveNullAndEmptyArrays': True
        }
    }, {
        '$lookup': {
            'from': 'users_details', 
            'localField': 'post_likes.user_id', 
            'foreignField': '_id', 
            'as': 'post_likes.user_info'
        }
    }, {
        '$lookup': {
            'from': 'users_details', 
            'localField': 'post_comments.user_id', 
            'foreignField': '_id', 
            'as': 'post_comments.user_info'
        }
    }, {
        '$unwind': {
            'path': '$post_likes.user_info', 
            'preserveNullAndEmptyArrays': True
        }
    }, {
        '$unwind': {
            'path': '$post_comments.user_info', 
            'preserveNullAndEmptyArrays': True
        }
    }, {
        '$group': {
            '_id': '$_id', 
            'post_created_user_id': {
                '$first': '$post_created_user_id'
            }, 
            'post_content': {
                '$first': '$post_content'
            }, 
            'post_likes': {
                '$push': '$post_likes'
            }, 
            'post_comments': {
                '$push': '$post_comments'
            }
        }
    }
]

獲取以下帶有重復值的 output。

[{
  "_id": {
    "$oid": "5f232c18a93ce29203b3f2f6"
  },
  "post_created_user_id": {
    "$oid": "5ed15463f43c335cb2e9ef73"
  },
  "post_content": "Content 1",
  "post_likes": [
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef75"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef75"
        },
        "first_name": "xxx3",
        "last_name": "xxx3",
        "email": "xxx3@gmail.com"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef75"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef75"
        },
        "first_name": "xxx3",
        "last_name": "xxx3",
        "email": "xxx3@gmail.com"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef76"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef76"
        },
        "first_name": "xxx4",
        "last_name": "xxx4",
        "email": "xxx4@gmail.com"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef76"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef76"
        },
        "first_name": "xxx4",
        "last_name": "xxx4",
        "email": "xxx4@gmail.com"
      }
    }
  ],
  "post_comments": [
    {
      "comment_id": {
        "$oid": "5f2850966efb9d4e803b2030"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
      },
      "comment_content": "Test comment1",
      "comment_date": {
        "$date": "2020-08-03T17:59:50.132Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef73"
        },
        "first_name": "xxx1",
        "last_name": "xxx1",
        "email": "xxx@gmail.com"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850ff6efb9d4e803b2033"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef74"
      },
      "comment_content": "test comments 1",
      "comment_date": {
        "$date": "2020-08-03T18:01:35.008Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef74"
        },
        "first_name": "xxx2",
        "last_name": "xxx2",
        "email": "xxx2@gmail.com"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850966efb9d4e803b2030"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
      },
      "comment_content": "Test comment1",
      "comment_date": {
        "$date": "2020-08-03T17:59:50.132Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef73"
        },
        "first_name": "xxx1",
        "last_name": "xxx1",
        "email": "xxx@gmail.com"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850ff6efb9d4e803b2033"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef74"
      },
      "comment_content": "test comments 1",
      "comment_date": {
        "$date": "2020-08-03T18:01:35.008Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef74"
        },
        "first_name": "xxx2",
        "last_name": "xxx2",
        "email": "xxx2@gmail.com"
      }
    }
  ]
},{
  "_id": {
    "$oid": "5f232c18a93ce29203b3f2f7"
  },
  "post_created_user_id": {
    "$oid": "5ed15463f43c335cb2e9ef73"
  },
  "post_content": "content 2",
  "post_likes": [
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef77"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef77"
        },
        "first_name": "xxx3",
        "last_name": "xxx3",
        "email": "xxx3@gmail.com"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef77"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef77"
        },
        "first_name": "xxx3",
        "last_name": "xxx3",
        "email": "xxx3@gmail.com"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef78"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef78"
        },
        "first_name": "xxx4",
        "last_name": "xxx4",
        "email": "xxx4@gmail.com"
      }
    },
    {
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef78"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef78"
        },
        "first_name": "xxx4",
        "last_name": "xxx4",
        "email": "xxx4@gmail.com"
      }
    }
  ],
  "post_comments": [
    {
      "comment_id": {
        "$oid": "5f2850966efb9d4e803b2030"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
      },
      "comment_content": "Test comment1",
      "comment_date": {
        "$date": "2020-08-03T17:59:50.132Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef73"
        },
        "first_name": "xxx1",
        "last_name": "xxx1",
        "email": "xxx@gmail.com"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850ff6efb9d4e803b2033"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef74"
      },
      "comment_content": "test comments 1",
      "comment_date": {
        "$date": "2020-08-03T18:01:35.008Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef74"
        },
        "first_name": "xxx2",
        "last_name": "xxx2",
        "email": "xxx2@gmail.com"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850966efb9d4e803b2030"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef73"
      },
      "comment_content": "Test comment1",
      "comment_date": {
        "$date": "2020-08-03T17:59:50.132Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef73"
        },
        "first_name": "xxx1",
        "last_name": "xxx1",
        "email": "xxx@gmail.com"
      }
    },
    {
      "comment_id": {
        "$oid": "5f2850ff6efb9d4e803b2033"
      },
      "user_id": {
        "$oid": "5ed15463f43c335cb2e9ef74"
      },
      "comment_content": "test comments 1",
      "comment_date": {
        "$date": "2020-08-03T18:01:35.008Z"
      },
      "user_info": {
        "_id": {
          "$oid": "5ed15463f43c335cb2e9ef74"
        },
        "first_name": "xxx2",
        "last_name": "xxx2",
        "email": "xxx2@gmail.com"
      }
    }
  ]
}]

有人可以幫我構建聚合嗎?

一步一步看,

post_comments 查找

  • $unwind post_comments 因為它是數組,我們需要通過單個評論查找
db.posts.aggregate([
  {
    $unwind: "$post_comments"
  },
  • $lookup你准備的是正確的
  {
    $lookup: {
      from: "users_details",
      localField: "post_comments.user_id",
      foreignField: "_id",
      as: "post_comments.user_info"
    }
  },
  • 從 user_info 中刪除了不需要的字段
  {
    $project: {
      "post_comments.user_info._id": 0
    }
  },
  • $unwind post_comments.user_info因為它是一個數組,我們只需要一個 user_info object
  {
    $unwind: {
      path: "$post_comments.user_info"
    }
  },
  • $group by _id因為上面的unwind把所有的文檔分開了,這里需要合並
  {
    $group: {
      _id: "$_id",
      post_created_user_id: {
        $first: "$post_created_user_id"
      },
      post_date: {
        $first: "$post_date"
      },
      post_content: {
        $first: "$post_content"
      },
      post_comments: {
        $push: "$post_comments"
      }
    }
  }
])

post_likes 查找

  • 重復上述步驟只需將post_comments更改為post_likes並將 $group 更改為 push post_likes ,您可以查看下面的游樂場。

您可以按順序組合上述查詢部分,為了解釋目的而分開。

游樂場: https://mongoplayground.net/p/GzVvEbPNwkA

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM