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
315 views
in Technique[技术] by (71.8m points)

mongodb - Mongo aggregation for Id which is set as a key?

I have a mongo database where I need to return populated data but I am not sure how to do it

Here are my collections:

Comment

{
  _id: 'someId',
  userId: 'someUserId',
  answers: {
    someQuestionId1: 'this is an answer to the question',
    someQuestionId2: 'this is another answer to the question'
  }
}

In the answers object each of the keys is an id for a question in a separate collection

Question

{
  _id: 'someQuestionId1',
  content: {
    en-GB: ['someQuestionVersion']
  }
}

In the question object the first value in the array is the most recent version of a question

Version

{
  _id: 'someQuestionVersion',
  content: {
    label: 'This is the question label'
  }
}

The label inside the version object is the piece of info I need.

I would like the aggregation to return an object that looks like this:

{
  _id: 'someId',
  userId: 'someUserId',
  answers: [{
    label: 'This is the question label',
    answer: 'this is an answer to the question'
  }, {
    label: 'This is another question label',
    answer: 'this is an another answer to the question'
  }  
    someQuestionId1: 'this is an answer to the question',
    someQuestionId2: 'this is another answer to the question'
  ]
}

Any ideas on how to do this?

question from:https://stackoverflow.com/questions/65602556/mongo-aggregation-for-id-which-is-set-as-a-key

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

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

1.4m articles

1.4m replys

5 comments

57.0k users

...