繁体   English   中英

$ lookup在mongodb中没有结果

[英]$lookup giving no results in mongodb

注意:在下面的编辑中,我直接使用mongo shell尝试了此操作,并更正了集合名称,但仍然存在相同的问题。

我目前正在尝试学习Node和Mongodb。 我想了解如何在查询中添加一个文档和另一个文档。 所有文档都指向$lookup

我设置了以下两个模型,它们各自可以完美运行

var BearSchema   = new Schema({
    name: String
});

module.exports = mongoose.model('Bear', BearSchema);

var CommentSchema   = new Schema({
    creator_id : { type: String, ref: 'Bear' },
    comment: String
});

module.exports = mongoose.model('Comment', CommentSchema);

我将省略其他设置细节,直接进入查询。

当我运行Bear.find()我得到了预期的结果...

    [
      {
        "_id": "585887a29b7915f437742b88",
        "name": "new bear",
        "__v": 0
      }
    ]

当我运行Comment.find()我得到了预期的结果...

[
  {
    "_id": "585887ae9b7915f437742b89",
    "creator_id": "584de876238179030d7d7916",
    "comment": "yoyoyo",
    "__v": 0
  },
  {
    "_id": "585887e09b7915f437742b8a",
    "creator_id": "585887a29b7915f437742b88",
    "comment": "ok lets give this a go",
    "__v": 0
  }
]

请注意,第二个注释中的creator_id与bear结果中的_id相同。

然后我跑

    Bear.aggregate([
        {
            $lookup: {
                from: "Comment",
                localField: "_id",
                foreignField: "creator_id",
                as: "comments"
            }
        }
    ], function (err, bears) {
        if (err)
            res.send(err);

        res.json(bears);
    });

并获得以下信息:

[
  {
    "_id": "585887a29b7915f437742b88",
    "name": "new bear",
    "__v": 0,
    "comments": []
  }
]

我希望出现以下情况:

[
  {
    "_id": "585887a29b7915f437742b88",
    "name": "new bear",
    "__v": 0,
    "comments": [      
       {
         "_id": "585887e09b7915f437742b8a",
         "creator_id": "585887a29b7915f437742b88",
         "comment": "ok lets give this a go",
         "__v": 0
       }
     ]
  }
]

在这种情况下,我无法理解它怎么知道"Comment"指的是什么。

编辑:从文档中,我可以看到from字段显示: Specifies the collection in the same database to perform the join with. The from collection cannot be sharded. Specifies the collection in the same database to perform the join with. The from collection cannot be sharded.

编辑2:在mongoshell中,我运行了以下查询及其结果,因为您可以看到即使使用正确的集合名称也仍然出现相同的问题,但是现在我可以看到ObjectId()可能是问题...

> show collections
bears
comments


> db.bears.find();
{ "_id" : ObjectId("585887a29b7915f437742b88"), "name" : "new bear", "__v" : 0 }

> db.comments.find();
{ "_id" : ObjectId("585887ae9b7915f437742b89"), "creator_id" : "584de87623817903
0d7d7916", "comment" : "yoyoyo", "__v" : 0 }
{ "_id" : ObjectId("585887e09b7915f437742b8a"), "creator_id" : "585887a29b7915f4
37742b88", "comment" : "ok lets give this a go", "__v" : 0 }


> db.bears.aggregate([ { $lookup: { from: "comments", localField: "_id", foreign
Field: "creator_id", as: "comments" } } ]);
{ "_id" : ObjectId("585887a29b7915f437742b88"), "name" : "new bear", "__v" : 0,
"comments" : [ ] }

每当使用$ lookup时,都必须在“来自”字段中添加一个额外的“ s”。 例如:如果您的表名是“ register”,那么您必须写“ registers”

注意:仅在$ lookup时

我解决了 有两个问题。

  1. Bear Schema _ id实际上是一个ObjectID()因此它没有正确比较两者。
  2. 我误解了集合名称是什么,因此Comment不会被认可。

解:

创建Comment模型时,我使用了Schema.ObjectId

var CommentSchema   = new Schema({
    creator_id : { type: Schema.ObjectId, ref: 'Bear' },
    comment: String
});

当执行查询时,我使用comments而不是Comment因为这是创建的名为Mongoose的集合。

    Bear.aggregate([
        {
            $lookup: {
                from: "comments",
                localField: "_id",
                foreignField: "creator_id",
                as: "comments"
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM