taieb
June 1, 2018, 10:58am
1
I want to do a Query by going through two different documents, is it possible to do it ?
Document A { _id, senders_ID[sender_id],email}
Document B { sender_id, firstname}
// i want to loop through document A and get each sender_id and go to document B to get the firstname.
I’m sure it is possible, but not quite understanding the schema of your Document A. Is senders_ID[sender_id]
an array field in the document? Probably would need you to post more code to give a good example as well. Are these documents in two different collections?
taieb
June 4, 2018, 2:30pm
3
Thank you for response let me explain it to you :
Document 1 :
{
"_id": "456d456d67exa",
"MovieName": "Forest",
"senders": [
"zAxNarwAE2AnvPb8P",
"Kz65arwAE2AnvPb8P",
"Walaik56YDvWMShKY",
"f2HA9Xc9hJNRuL5cp"
],
"version": "4.2.0",
"hardware": "5",
}
I want to go through the senders List
and do somehting like this
Promise.all(current_box.senders.map(sender => Meteor.users.findOne({ "_id": sender })))
.then((UserLists) => {
// Deals with ok
console.log(UserLists)
for (var i=0; i<UserLists.length; i++)
for (var profile in UserLists[i]) {
console.log("Name of user: "+UserLists[i][profile].name);
}
})
.catch((err) => {
console.log(err)
});
Exemple of a the document of the "_id": "zAxNarwAE2AnvPb8P",
that i want to get the emails.adress + profile.name
{
"_id": "zAxNarwAE2AnvPb8P",
"createdAt": {
"$date": "2018-04-16T09:45:10.212Z"
},
"emails": [
{
"address": "Jean@paul.com",
"verified": false
}
],
"profile": {
"features": {
"pictures": false
},
"name": "Jean-Paul"
},
}
I want to get in a table the MovieName + the senders ( email + name )
One way to do it.
In gql schema for current_box:
type CurrentBox {
_id: ID,
MovieName: String,
senders: senderProfiles,
version: String,
hardware: String
}
In the resolver for current_box:
CurrentBox: {
senderProfiles(root) {
return Meteor.users.find({ _id: { $in: root.senders } }, { fields: { emails: 1, profile: 1 } }).fetch();
}
},
It replaces your senders array with the data array of user profiles you wanted for the query.
See this warning about profile: https://guide.meteor.com/accounts.html#dont-use-profile
taieb
June 5, 2018, 8:29am
5
Sir please i updated my question here Nested query returns empty array