0

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?

Hamada
  • 1,836
  • 3
  • 13
  • 27
I_A
  • 331
  • 2
  • 14
  • 1
    Depends on the data size and various other details. I suggest focusing on maintainability and readability of your implementation. Keep in mind MongoDB documents cannot exceed 16mb in size. – D. SM Jul 05 '20 at 02:39
  • 1
    I'd suggest going with just the participation on users, or just having it on board. Maintaining the same data in 2 places could lead to discrepancies. To actually answer your question though, the `_id` field is just an indexed field, so there should be no difference. – Plancke Jul 06 '20 at 05:54

0 Answers0