【问题标题】:$lookup(Aggregation) using pymongo$lookup(聚合) 使用 pymongo
【发布时间】:2016-10-14 22:01:40
【问题描述】:

以下是我拥有的两个系列:

db.try1.insert([
    {id: 123, product:"earphones", ref:"asdf"},
    {id: 321, product:"speaks", ref:"zxcv"}
])

db.try2.insert([
    {rf:"zxcv", Owner:"Jeff"},
    {rf:"asdf", Owner:"John"}
])

我使用了以下聚合语句

cursor_3=db.try1.aggregate([{
    "$lookup":{
        "from":"try2",
        "localField":"ref",
        "foreignField":"rf",
        "as":"rightstuff"
    }
}])

但是我没有得到想要的输出。数组权限保持空白。 我正在使用 Mongodb 3.2 和 pymongo 驱动程序。

修复它。

【问题讨论】:

  • 您的查询没问题。
  • 嗨 Spartan07 - 如果你解决了你的问题,你能把它作为答案发布吗?这样,您可以让其他读者知道如何解决类似的问题;它还会将您的问题从未回答的列表中删除。

标签: mongodb pymongo


【解决方案1】:

如果是 pymongo 代码,则应将字段名称用引号括起来,如下所示:

db.try1.insert([
    {'id': 123, 'product':"earphones", 'ref':"asdf"},
    {'id': 321, 'product':"speaks", 'ref':"zxcv"}
])

db.try2.insert([
    {'rf':"zxcv", 'Owner':"Jeff"},
    {'rf':"asdf", 'Owner':"John"}
])

您的聚合查询代码是正确的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2021-06-04
    • 2023-03-09
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 2020-01-15
    相关资源
    最近更新 更多