简体   繁体   English

$ lookup在mongodb中没有结果

[英]$lookup giving no results in mongodb

Note: Edits below where I tried this directly using mongo shell and correct collection names, but still the same issue. 注意:在下面的编辑中,我直接使用mongo shell尝试了此操作,并更正了集合名称,但仍然存在相同的问题。

I am currently trying to learn Node and Mongodb. 我目前正在尝试学习Node和Mongodb。 I am looking to understand how to add one document with another in a query. 我想了解如何在查询中添加一个文档和另一个文档。 All the documentation points back to $lookup . 所有文档都指向$lookup

I have the two following models set up, which both work perfectly on their own 我设置了以下两个模型,它们各自可以完美运行

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);

I will omit other set up details and get straight to the queries. 我将省略其他设置细节,直接进入查询。

When I run Bear.find() I get the expected result... 当我运行Bear.find()我得到了预期的结果...

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

When I run Comment.find() I get the expected result... 当我运行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
  }
]

Note the creator_id in the second comment is the same as the _id in the bear result. 请注意,第二个注释中的creator_id与bear结果中的_id相同。

I then run 然后我跑

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

        res.json(bears);
    });

and get the following: 并获得以下信息:

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

I was hoping the following would appear: 我希望出现以下情况:

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

I cant understand in this situation how it would know what "Comment" is referring to. 在这种情况下,我无法理解它怎么知道"Comment"指的是什么。

EDIT: From the documentation I can see the from field says: Specifies the collection in the same database to perform the join with. The from collection cannot be sharded. 编辑:从文档中,我可以看到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.

EDIT 2: In mongoshell I have ran the following queries and their results, as you can see the same issue is still appearing even with the correct collection name, however I can now see ObjectId() may be the issue... 编辑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" : [ ] }

whenever you'r using $lookup, you must add an extra "s" in "from" field. 每当使用$ lookup时,都必须在“来自”字段中添加一个额外的“ s”。 for example: if your table name is "register" then you have to write "registers" 例如:如果您的表名是“ register”,那么您必须写“ registers”

Note: at the time of $lookup only 注意:仅在$ lookup时

I resolved this. 我解决了 There were two issues. 有两个问题。

  1. The Bear Schema _ id is actually an ObjectID() so it wasnt comparing the two correctly. Bear Schema _ id实际上是一个ObjectID()因此它没有正确比较两者。
  2. I misunderstood what collection names were and so Comment would not have been recognised. 我误解了集合名称是什么,因此Comment不会被认可。

Solution: 解:

When creating the Comment Model I used Schema.ObjectId 创建Comment模型时,我使用了Schema.ObjectId

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

When doing the query I used comments instead of Comment as this was the collection named Mongoose created. 当执行查询时,我使用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