1

I insert a new document in Votes collection when a user votes on a poll.

{
    _id: ObjectId(XXX),
    card: 11,
    user: 22
}

Now when a user requests for all the polls I want to return a Voted: 1 field if the users have already voted on the poll i.e. a document is already present in the Votes collection.

Can anyone tell me if there's a way to access documents from another collection in aggregation command.

Paul Wasilewski
  • 9,762
  • 5
  • 45
  • 49
Chaitanya
  • 1,440
  • 1
  • 14
  • 26
  • Don't forget to mark the answer accepted whenever it helped (most) in solving the problem. See also http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235 – Paul Wasilewski Apr 25 '16 at 10:22

1 Answers1

0

With mongoDB it's not possible to access multiple documents within a query. You should change your data model and add an array or use embedded documents.

I don't know much about your use case so please take this just as an example and not as a final solution.

The following model contains an array for all voted polls of an user. Therefore you can check if the array contains the poll and return 1 if its true.

{
    _id: ObjectId(XXX),
    user: 22,
    cards: [1, 3, 5]
}

See https://docs.mongodb.org/manual/core/data-modeling-introduction/ for more details about data modelling in mongoDB.

Paul Wasilewski
  • 9,762
  • 5
  • 45
  • 49