2016-12-20 6 views
0

注:これはmongoシェルと正しいコレクション名を使って直接試したところで編集しますが、同じ問題です。

現在、ノードとMongodbを学習しようとしています。私は1つのドキュメントを別のドキュメントに追加する方法を理解しています。すべてのドキュメントは$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); 

で完璧に動作設定2次のモデルを、持っています。

私はBear.find()を実行すると、私は期待どおりの結果を得る...

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

私はComment.find()を実行すると私は2番目のコメントでcreator_idが期待される結果...

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

注意を取得クマの結果の_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"を参照してください。

EDIT:あなたは同じ問題が依然としても表示されている見ることができるよう、mongoshellで私は次のクエリとその結果を走っています:Specifies the collection in the same database to perform the join with. The from collection cannot be sharded.

EDIT 2:documentationから私はfromフィールドは言う見ることができます正しいコレクション名ですが、今は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" : [ ] } 
+0

を検索する時に外国のフィールド「creator__idでそのタイプミスです"for" creator_id "?また、ローカルフィールドは_idでなければなりません。 – Veeram

+0

申し訳ありませんが、ここからは、問題があるかどうかを確認するために交換しています。 – Adrian

+0

私はmongoシェルで同じクエリを実行しました。私のためにうまくいった。 'db.Bear.aggregate([{ $ルックアップ:{から : "コメント"、 localField: "_id"、 foreignField: "creator_id"、 として: "コメント" }} ])' – Veeram

答えて

0

解決済みです。 2つの問題がありました。

  1. ベアスキーマ_ idは実際にはObjectID()です。したがって、2つを正しく比較していませんでした。
  2. コレクションの名前が間違っていたので、Commentは認識されませんでした。

ソリューション:

これはマングースが作成した名前付きのコレクションだったので、私はcomments代わりのCommentを使用したクエリを実行する場合、私はSchema.ObjectId

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

を使用Commentモデルを作成します。

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

$ lookupを使用するときはいつでも、「from」フィールドに余分な「s」を追加する必要があります。例えば : あなたのテーブル名が であるならば、あなたは を記述する必要が を「登録」

注意「を登録」:$のみ

関連する問題