I'd go with the array of tickets being stored in users. As far as I know, Mongo doesn't really have a way to query one collection based on the (lack of) elements in another collection. With the array, though, you can simply do db.users.find({tickets:[]})
.
Retrieve records in mongoDB using bidirectional query
-
31-05-2022 - |
質問
I have two collections - Tickets
and Users
. Where a user
can have one to many tickets
. The ticket collection is defined as follows
Ticket = {_id, ownerId, profile: {name}}
The ownerId is used to find all tickets that belong to a specific person. I need to write a query that gets me all users with no tickets.
How can i write this query without having to loop through all users, checking if the userID shows up in any Tickets?
Would a bidirectional storage cause me any performance problems ? For example, if i were to change my users
collection and add an array of tickets: [ticketID, ticketID2, ...]
?
解決
所属していません StackOverflow