I have two collections in MongoDB
Users (find()):
{ "_id" : ObjectId("5021314e372c50859ced7063"), "name" : "Vinícius", "username" : "vinixhenri", "password" : "vinix123" }
{ "_id" : ObjectId("50213180372c50859ced7064"), "name" : "André", "username" : "andre", "password" : "andre123" }
and Projects (findOne()):
{
"_id" : ObjectId("50215d09ddf7410905000000"),
"name" : "Projeto X",
"local" : "/home/server/compartilhamento/htdocs/project_x/",
"permissions" : [
{
"user" : "5021314e372c50859ced7063",
"alter" : true,
"view" : true,
"delete" : true,
"view_task" : true,
"insert_task" : true,
"alter_task" : true,
"delete_task" : true
},
{
"user" : "50213180372c50859ced7064",
"alter" : true,
"view" : true,
"delete" : false,
"view_task" : false,
"insert_task" : false,
"alter_task" : false,
"delete_task" : false
}
],
"tasks" : [
{
"responsible" : "5021314e372c50859ced7063",
"description" : "Ajuste na barra lateral do site"
},
{
"responsible" : "5021314e372c50859ced7063",
"description" : "Ajuste no cabeçalho do site"
},
{
"responsible" : "50213180372c50859ced7064",
"description" : "Ajuste no rodapé do site"
}
]
}
I showed up a findOne() collection of projects, but I wonder how it would be a find() also.
How would I do to get all the tasks where the charge has the username “vinixhenri”? The user identifier is tasks-> responsible.
I wonder also if I’m doing right in keeping the _id user? It is correct I have to consult the user information and then re-queries the database for project information? Or should I duplicate information about users’ tasks in the collection?
The most appropiate way until 2.2 (where you would have: https://jira.mongodb.org/browse/SERVER-828 or even the aggregation framework) would be to either:
Return all Root documents with one or more tasks with the “charge” of the particular user filtering them out on client side to get only those tasks across those project associated with that user which could work really well.
Run an MR (not so recommended) which will, once every so oftern, reformat your documents to the perpestive you need.
Note: The MR would not be realtime however you can make the client side filtering realtime as such I would personally choose to filter this out on client side.
Yes this is fine and performant. For example say that the user changes their username you don’t want to go through all the project docs changing their username do you? Having to query for an external row retrieving all information updates comes with it’s expenses but also it benefits. In this case it would be bad to replicate the users data onto the project document.