Question

I'm building sort of a question and answer system for my Meteor project. I have 3 collections: one named "Questions," one named "Answers," and one for admins to manually determine a question of the day, named "Today."

All collected answers will reference the question of the day. What I'm trying to figure out is how to make an insert statement for the "Answers" collection. Among the fields, it should add the ObjectId of the question being stored in "Today."

Answers.insert({
  user: Meteor.userId(), 
  date: new Date(),
  answer: answer,
  questions_id: **here is where I would like the ObjectId of the current question**
})

How can I reference and insert an ObjectId value, belonging to a field (let's call the field QOD) within the Today collection? If you have an answer with an actual example, it would be great. I'm not so experienced that someone can describe a solution to me without showing it. Thanks.

Of course, I'll also have to figure out how to publish this stuff later. But I'll cross that road when I get there.

Was it helpful?

Solution

If Answers needs to reference a record from Questions, then you need to obtain that record and include the _id field in the object you'll insert in Answers:

var question = Questions.findOne({...criteria...});
Answers.insert({
  user: Meteor.userId(),
  date: new Date(),
  answer: answer,
  questionId: question._id
});

Hope this helps. I see "Entries" instead of "Answers" in your question though so you may want to clarify. Also, instead of a separate "Today" collection, you may want to have a separate field in the Questions collection which would mark certain questions as being selected as "Question of the Day".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top