使用ObjectId的mongodb聚合查找

使用ObjectId的mongodb聚合查找,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,在mongodb中,我正在努力进行一个基本的查找。在尝试了多种组合后,我不知道出了什么问题。我发现有很多问题需要解决,但在尝试了所有答案后,什么都不起作用 这是我的用户收藏 { "_id": { "$oid": "5b268395c176db1548bd92c2" }, "title": "Item #1", "description": "Item 1 Description", "categories": [ { "$ref": "catego

在mongodb中,我正在努力进行一个基本的查找。在尝试了多种组合后,我不知道出了什么问题。我发现有很多问题需要解决,但在尝试了所有答案后,什么都不起作用

这是我的用户收藏

{
  "_id": {
    "$oid": "5b268395c176db1548bd92c2"
  },
  "title": "Item #1",
  "description": "Item 1 Description",
  "categories": [
    {
      "$ref": "categories",
      "$id": {
        "$oid": "5b268248c176db1548bd92af"
      }
    }
  ],
  "status": "borrowed",
  "image": "http://cdn.site.com/testimage.png",
  "borrower": "5b2684a0c176db1548bd92d5",
  "owner": {
    "$ref": "users",
    "$id": {
      "$oid": "5aecc8012d3d947ba130800d"
    }
  }
}

{
  "_id": {
    "$oid": "5b2c2c4d3c70af2da07d1266"
  },
  "title": "Item 2",
  "description": "Item 2 Description",
  "categories": [
    {
      "$ref": "categories",
      "$id": {
        "$oid": "5b268248c176db1548bd92af"
      }
    }
  ],
  "status": "Available",
  "image": "http://cdn.site.com/testimage1.png",
  "borrower": null,
  "owner": {
    "$ref": "users",
    "$id": {
      "$oid": "5b2684a0c176db1548bd92d5"
    }
  }
}

这是我的收藏

{
  "_id": {
    "$oid": "5b268395c176db1548bd92c2"
  },
  "title": "Item #1",
  "description": "Item 1 Description",
  "categories": [
    {
      "$ref": "categories",
      "$id": {
        "$oid": "5b268248c176db1548bd92af"
      }
    }
  ],
  "status": "borrowed",
  "image": "http://cdn.site.com/testimage.png",
  "borrower": "5b2684a0c176db1548bd92d5",
  "owner": {
    "$ref": "users",
    "$id": {
      "$oid": "5aecc8012d3d947ba130800d"
    }
  }
}

{
  "_id": {
    "$oid": "5b2c2c4d3c70af2da07d1266"
  },
  "title": "Item 2",
  "description": "Item 2 Description",
  "categories": [
    {
      "$ref": "categories",
      "$id": {
        "$oid": "5b268248c176db1548bd92af"
      }
    }
  ],
  "status": "Available",
  "image": "http://cdn.site.com/testimage1.png",
  "borrower": null,
  "owner": {
    "$ref": "users",
    "$id": {
      "$oid": "5b2684a0c176db1548bd92d5"
    }
  }
}

每个项目都有一个所有者,该所有者映射到用户集合中用户的_id。 对于我的查询,我正在尝试使用相应的用户获取项目。这就是问题所在

   db.items.aggregate([
    {
        "$lookup" : {
          localField : "owner",
          from : "users",
          foreignField : "_id",
          as : "users"
        }
    }
])
它返回这个-

我尝试过不同的变体,比如

db.items.aggregate([
    {
        "$lookup" : {
          localField : "owner.str",
          from : "users",
          foreignField : "_id.str",
          as : "users"
        }
    }
])
这会导致用户数组填充不正确(与所有用户一起!)

我做错了什么

编辑

以下是项目集合

{
  "_id": {
    "$oid": "5b268395c176db1548bd92c2"
  },
  "title": "Item #1",
  "description": "Item 1 Description",
  "categories": [
    {
      "$ref": "categories",
      "$id": {
        "$oid": "5b268248c176db1548bd92af"
      }
    }
  ],
  "status": "borrowed",
  "image": "http://cdn.site.com/testimage.png",
  "borrower": "5b2684a0c176db1548bd92d5",
  "owner": {
    "$ref": "users",
    "$id": {
      "$oid": "5aecc8012d3d947ba130800d"
    }
  }
}

{
  "_id": {
    "$oid": "5b2c2c4d3c70af2da07d1266"
  },
  "title": "Item 2",
  "description": "Item 2 Description",
  "categories": [
    {
      "$ref": "categories",
      "$id": {
        "$oid": "5b268248c176db1548bd92af"
      }
    }
  ],
  "status": "Available",
  "image": "http://cdn.site.com/testimage1.png",
  "borrower": null,
  "owner": {
    "$ref": "users",
    "$id": {
      "$oid": "5b2684a0c176db1548bd92d5"
    }
  }
}
以下是用户收藏:

{
  "_id": {
    "$oid": "5aecc8012d3d947ba130800d"
  },
  "email": "n.y@gmail.com",
  "registeredOn": "20/10/2018 12:12:29 AM",
  "avatarURL": "http://gravtar.com/12334",
  "friends": []
}

{
  "_id": {
    "$oid": "5b2684a0c176db1548bd92d5"
  },
  "email": "j.v@gmail.com",
  "registeredOn": "20/10/2018 12:12:29 AM",
  "avatarURL": "http://gravtar.com/12334",
  "friends": [],
  "wishlist": [
    {
      "$ref": "items",
      "$id": {
        "$oid": "5b2831543c70af2da07d11da"
      }
    }
  ]
}

如果您想用相应的用户填充
items
记录,我想您可以使用mongoose
find
API中的
populate
选项

示例代码:

this.read = function(query, populate, onSuccess, onFailure, forcePull, sortQuery, ttl, limitValue, selectQuery) {
        var scope = this;
        logger.info(this.TAG, "Read operation execution started on ORM Object: "
                    + modelName + " with query: " + JSON.stringify(query));
        if (query) {

            var queryObject = this._model.find(query);
            if (populate) {
              queryObject = queryObject.populate(populate);
            }
            if (selectQuery) {
              queryObject = queryObject.select(selectQuery);
            }
            if (sortQuery) {
              queryObject = queryObject.sort(sortQuery);
            }
            if (limitValue) {
              queryObject = queryObject.limit(limitValue);
            }
            queryObject.exec(function(error, records) {
                if (error) {
                    logger.error(scope.TAG, "Error while ORM read: " + JSON.stringify(error));
                    onFailure(error);
                    return;
                }
                logger.info(scope.TAG, "Record read successfully: " + JSON.stringify(records));
                onSuccess(records);
            });
          }
     }

您可以将populate作为字符串数组传递,如
['owner']
在本例中

如果您想用相应的用户填充
记录,我认为您可以使用mongoose
find
API中的
populate
选项

示例代码:

this.read = function(query, populate, onSuccess, onFailure, forcePull, sortQuery, ttl, limitValue, selectQuery) {
        var scope = this;
        logger.info(this.TAG, "Read operation execution started on ORM Object: "
                    + modelName + " with query: " + JSON.stringify(query));
        if (query) {

            var queryObject = this._model.find(query);
            if (populate) {
              queryObject = queryObject.populate(populate);
            }
            if (selectQuery) {
              queryObject = queryObject.select(selectQuery);
            }
            if (sortQuery) {
              queryObject = queryObject.sort(sortQuery);
            }
            if (limitValue) {
              queryObject = queryObject.limit(limitValue);
            }
            queryObject.exec(function(error, records) {
                if (error) {
                    logger.error(scope.TAG, "Error while ORM read: " + JSON.stringify(error));
                    onFailure(error);
                    return;
                }
                logger.info(scope.TAG, "Record read successfully: " + JSON.stringify(records));
                onSuccess(records);
            });
          }
     }

您可以将populate作为字符串数组传递,如
['owner']
,在您的情况下
本地字段
外来字段
应该是相同的数据类型。您分别是
DBRef
ObjectId
localField
foreignField
应该是相同的数据类型。您分别是
DBRef
ObjectId

亲爱的Jay,您已经在这里呆了一段时间,因此您必须熟悉和。请发布两个集合中的示例JSON文档,而不是屏幕截图。理想情况下,如果你去掉不必要的字段。@AlexBlex谢谢-我认为我一定是在查询时犯了一个基本的错误,熟悉mongo的人只要看一下树结构就能很容易地分辨出来。无论如何,用json示例更新post。现在,停止跟踪我的个人资料;-)查询本身没有问题,但由于没有得到预期的结果,它可能与数据有关,尤其是
owner
字段。
localField
foreignField
应该与数据类型一致,在您的屏幕截图上,一个是Reference,另一个是ObjectId。@AlexBlex观察得很好。我假设引用是ObjectId类型的。我的错。是否需要将所有者字段更改为“ObjectId”类型?或者有没有一种方法可以比较引用和ObjectId类型?如果可以,可以。与使用DBRef的优点相比,DBRef有太多的问题:例如,这里有一个重复的问题,所以:亲爱的Jay,您已经在这里呆了一段时间,所以您必须熟悉和。请发布两个集合中的示例JSON文档,而不是屏幕截图。理想情况下,如果你去掉不必要的字段。@AlexBlex谢谢-我认为我一定是在查询时犯了一个基本的错误,熟悉mongo的人只要看一下树结构就能很容易地分辨出来。无论如何,用json示例更新post。现在,停止跟踪我的个人资料;-)查询本身没有问题,但由于没有得到预期的结果,它可能与数据有关,尤其是
owner
字段。
localField
foreignField
应该与数据类型一致,在您的屏幕截图上,一个是Reference,另一个是ObjectId。@AlexBlex观察得很好。我假设引用是ObjectId类型的。我的错。是否需要将所有者字段更改为“ObjectId”类型?或者有没有一种方法可以比较引用和ObjectId类型?如果可以,可以。与使用DBRef的优点相比,DBRef有太多的问题:例如,这里有一个重复的问题:我没有使用mongoose-我将使用java驱动程序。尝试在mongo中首先使用MongoShell获得正确的查询,然后将其移动到Java。我没有使用mongoose-我将使用Java驱动程序。尝试使用mongo shell在mongo中获得正确的查询,然后将其移动到Java。谢谢,您保存了我的头发。。再拖两天头发,我就会秃顶了!谢谢你,你救了我的头发。。再拖两天头发,我就会秃顶了!