简体   繁体   中英

$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.

I am currently trying to learn Node and Mongodb. I am looking to understand how to add one document with another in a query. All the documentation points back to $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...

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

When I run Comment.find() I get the expected result...

[
  {
    "_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.

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.

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. 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...

> 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. for example: if your table name is "register" then you have to write "registers"

Note: at the time of $lookup only

I resolved this. There were two issues.

  1. The Bear Schema _ id is actually an ObjectID() so it wasnt comparing the two correctly.
  2. I misunderstood what collection names were and so Comment would not have been recognised.

Solution:

When creating the Comment Model I used 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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