db.collection.find( { users: userID } ) vs $in operator for userItems

16 views
Skip to first unread message

William Simetin-Grenon

unread,
Oct 31, 2018, 9:35:50 PM10/31/18
to mongodb-user

I was wondering what would be the most efficient method to query a user’s items.


I could query for all items containing the user's id with db.collection.find( { users: userID } ) or store each item id of a user in the user itself and query them from that array with db.collection.find({ "_id": { "$in": [userItemsIDs] } }).

Is one method better than the other?

Oleg Toropov

unread,
Oct 31, 2018, 10:02:39 PM10/31/18
to mongod...@googlegroups.com
Since MongoDB is document oriented DB, it is assumed that you keep all documents for user in one place (there is a limit on the size of the record - 16MB).
Simple, right? But the answer is more complicated.
1) Obviously if most of the queries will need all items than keep them all in one pace. It is easy to maintain the integrity of the record (update, delete)
2) If there are some items that will be used rarely in queries, or the items are  too big and cannot be kept in one record, then you _have_to_ design the schema to optimize your application.

I would recommend to take free training with MongoDB University 



--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/a8d5eaf0-cb40-4c0f-9750-ba9cc75d5801%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
//
// Oleg V.Toropov, MCP | oleg.t...@gmail.com
// -----------------------------------------------------------------
// I have discovered that if I read enough stuff that's over my head,
// I actually begin to understand some of it.
//

Reply all
Reply to author
Forward
0 new messages