Is querying a document by ID faster than querying by embedded document in MongoDB

71 Views Asked by At

In my app, I have a User collection and a Board collection. A user document has an embedded participation document, which looks like this: {_id: 1, board_id: 1, role: "admin"}. (A participation is effectively a join between the board and a user)

I am trying to decide how I should get all the user's with participation in a particular board the board.users method:

  1. Query by embedded participation document: db.users.find( { 'participation.board_id': board.id }
  2. Maintain a array of user_ids on a board document: db.users.find( { _id: { $in: board.user_ids } } )

Assuming the user.participations.board_id the field is also indexed, is maintaining a user_ids array on board going to result in faster performance?

In essence, Is querying a document by ID faster than querying by an embedded document?

0

There are 0 best solutions below