簡體   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