Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

join - MongoDB add to joining collection field from base one

I have two collections:

Games with schema:

 _id: ObjectId('gameId'),
 questions: [
   {
     position: 1,
     question_id: ObjectId('baz')
   },
   {
     position: 2,
     question_id: ObjectId('ban')
   },
 ]

Questions with schema:

 _id: ObjectId('baz'),
 text: 'FooBar'

And now I'd like to join questions to games with adding to each question record value of question_position.

So, I have query like this:

db.games.aggregate([
    {
      $lookup: {
        from: 'questions',       
        localField: 'questions.question_id',
        foreignField: '_id',       
        as: 'question_data',     
      },   
     }])

Which return me all required info, with correct join according to questions array,

 _id: ObjectId('gameId'),
 questions: [
   {
     position: 1,
     question_id: ObjectId('baz')
   },
   {
     position: 2,
     question_id: ObjectId('ban')
   }
 ],
 question_data: [
   {
     _id: ObjectId('baz'),
     text: 'FooBar',
   },
   {
     _id: ObjectId('ban'),
     text: 'FooBar2',
   }
 ]

but I'm totally can't figure out how to add into joined questions it's position according to game. To look it like this:

 _id: ObjectId('gameId'),
 questions: [
   {
     position: 1,
     question_id: ObjectId('baz')
   },
   {
     position: 2,
     question_id: ObjectId('ban')
   }
 ],
 question_data: [
   {
     _id: ObjectId('baz'),
     text: 'FooBar',
     position: 1,
   },
   {
     _id: ObjectId('ban'),
     text: 'FooBar2',
     position: 2,
   }
 ]


I've tried with $unwind for question array in game collection, played a little with $project in aggregation but still no result.

So, my question is, how to add field from base collection to joined data from another collection

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to first $unwind the questions array and then need to apply $lookup and finally use $group to rollback again into the array.

db.games.aggregate([
  { "$unwind": "$questions" },
  { "$lookup": {
    "from": "questions",
    "localField": "questions.question_id",
    "foreignField": "_id",
    "as": "question_data"
  }},
  { "$unwind": "$question_data" },
  { "$addFields": {
    "question_data.position": "$questions.position",
    "question_data.question_id": "$questions.question_id"
  }},
  { "$group": {
    "_id": "$_id",
    "questions": { "$push": "$questions" },
    "question_data": { "$push": "$question_data" }
  }}
])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...