1

$ Lookup auf eingebettete Dokument Feld verwenden Sie einen Blick haben, werden Ihre HilfeWie bitte in mogodb

var user = new Schema({ 
     name: String, 

    }); 

var Comments = new Schema({ 
    title  : String 
    , body  : String 
    ,user_id : {type: Schema.Types.ObjectId, ref: 'user' } 
    , date  : Date 

}); 


var blog = new Schema({ 
    author : String 
    , title  : String 
    , body  : String 
    , date  : Date 
    , user_id :{type: Schema.Types.ObjectId, ref: 'user' } 
    , comments : [Comments] 

}); 



db.blogs.aggregate([ 
    { $match : { "_id" : ObjectId("57e3b7f4409d80a508d52769") } }, 

{ $lookup: {from: "users", localField: "user_id", foreignField: "_id", as: "User"} }, 

]) 

diese zurück

[ 
    { 
    "_id": "57e3b7f4409d80a508d52769", 
    "author": "Tariq", 
    "title": "MyfirstPost", 
    "body": "This is my first post", 
    "user_id": "57e3b763f7bc810c08f9467a", 
    "comments": [ 
     { 
     "title": "hi", 
     "body": "again i am commenting on this", 
     "user_id": "57e3b763f7bc810c08f9467a", 
     "_id": "57e3c153409d80a508d5276b" 
     }, 
     { 
     "title": "hi", 
     "body": "this is seond comment", 
     "user_id": "57e3b763f7bc810c08f9467a", 
     "_id": "57e3c8632ebca0ee0afb2ac6" 
     } 
    ], 
    "__v": 0, 
    "User": [ 
     { 
     "_id": "57e3b763f7bc810c08f9467a", 
     "name": "Tariq", 
     "username": "teekay", 
     "password": "123456", 
     "__v": 0 
     } 
    ] 
    } 
] 

das Rückergebnis durch Vergleich Blog-Tabelle ist und Benutzer appriciated Tabelle _id, die in Ordnung ist .. aber ich möchte Benutzerdetail mit jedem Kommentar mithilfe von user_id der "comments.user_id" -Blogsammlung und "_id" der Sammlung sollte so etwas wie

"_id": "57e3b7f4409d80a508d52769", 
    "author": "Tariq", 
    "title": "MyfirstPost", 
    "body": "This is my first post", 
    "user_id": "57e3b763f7bc810c08f9467a", 
    "comments": [ 
     { 
     "title": "hi", 
     "body": "again i am commenting on this", 
     "user_id": "57e3b763f7bc810c08f9467a", 
     "_id": "57e3c153409d80a508d5276b", 
    "User": [ 
     { 
     "_id": "57e3b763f7bc810c08f9467a", 
     "name": "Tariq", 
     "username": "teekay", 
     "password": "123456", 
     "__v": 0 
     } 
    ] 
     }, 

Antwort

1

Sie können eine Aggregation Betrieb der Pipeline laufen:

db.blogs.aggregate([ 
    { "$unwind": "$comments" }, 
    { 
     "$lookup": { 
      "from": "users", 
      "localField": "comments.user_id", 
      "foreignField": "_id", 
      "as": "comments.user" 
     } 
    }, 
    { "$unwind": "$comments.user" }, 
    { 
     "$group": { 
      "_id": "$_id", 
      "author": { "$first": "$author" }, 
      "title": { "$first": "$title" }, 
      "body": { "$first": "$body" }, 
      "comments": { "$push": "$comments" }, 
      "user_id": { "$first": "$user_id" } 
     } 
    }, 
    { 
     "$lookup": { 
      "from": "users", 
      "localField": "user_id", 
      "foreignField": "_id", 
      "as": "user" 
     } 
    }, 
    { "$unwind": "$user" }, 
])