What Logical Query Operator/s do I use to join TWO queries yielding documents that belong to the logged in user Meteor.userId();
AND all documents that have the logged in user's phone number in the recipientNumber
variable.
Document's holding the logged in users phone number in the recipientNumber
variable, suggests that the logged in user's are a Recipient of funds.
For reference, find below a logged in users details:
var user = Meteor.userId();
var userDetails = Meteor.users.findOne({_id: user}, { fields: { "profile.telephoneNumber": 1, "profile.name": 1 } } );
var usersNumber = userDetails.profile.telephoneNumber;
var userName = userDetails.profile.name;
console.log("Users _id is: " +user);
console.log("Users Number is: " +usersNumber);
console.log("Users Name is: " +userName);
The code above correctly yields:
Users _id is: "sxY3fsY9LmJYqFkiG"
Users Number is: +254705086633
Users Name is: Sir.BT
recipientsDetails.find({ }).fetch();
shows the contents of the collection in subject:
0:
payersName: "The LucidOreo"
payersUserId: "KDJFGkCRF3E5nY37e"
recipientNumber: "+254705086633"
_id: "MMyinrcrrYpz2haxq"
1:
payersName: "Sir.BT"
payersUserId: "sxY3fsY9LmJYqFkiG"
recipientNumber: "+254705086633"
_id: "YbPFT9ecCA4FTgqsn"
2:
payersName: "Sir.BT"
payersUserId: "sxY3fsY9LmJYqFkiG"
recipientNumber: "+254705577633"
_id: "ZPWNsQ7TWSJcERSJv"
The query below filters the collection, to show only the documents that belong to the user based on the users Meteor.userId() being: sxY3fsY9LmJYqFkiG.
var user = Meteor.userId();
recipientsDetails.find( {payersUserId: user} ).fetch();
Find below the results:
0:
payersName: "Sir.BT"
payersUserId: "sxY3fsY9LmJYqFkiG"
recipientNumber: "+254705086633"
_id: "YbPFT9ecCA4FTgqsn"
1:
payersName: "Sir.BT"
payersUserId: "sxY3fsY9LmJYqFkiG"
recipientNumber: "+254705577633"
_id: "ZPWNsQ7TWSJcERSJv"
I would like to formulate a query that shows a combination of TWO things:
All the documents that belong to the logged in user (as illustrated in code below):
var user = Meteor.userId();
recipientsDetails.find( {payersUserId: user} ).fetch();
...and all the documents that carry the phone number of the logged in user (also as illustrated in code below):
var user = Meteor.userId();
var userDetails = Meteor.users.findOne({_id: user}, { fields {"profile.telephoneNumber": 1 } } );
var usersNumber = userDetails.profile.telephoneNumber;
recipientsDetails.find( {recipientNumber: usersNumber} ).fetch();
What Logical Query Operator/s should I use in a query to yield this?
I have tried out:
var user = Meteor.userId();
var userDetails = Meteor.users.findOne({_id: user}, { fields: { "profile.telephoneNumber": 1 } } );
var usersNumber = userDetails.profile.telephoneNumber;
recipientsDetails.find( { $and: [{payersUserId: user}, {recipientNumber: usersNumber } ] } ).fetch();
Unfortunately this erroneously yields:
0:
payersName: "Sir.BT"
payersUserId: "sxY3fsY9LmJYqFkiG"
recipientNumber: "+254705086633"
_id: "YbPFT9ecCA4FTgqsn"
1:
payersName: "Sir.BT"
payersUserId: "sxY3fsY9LmJYqFkiG"
recipientNumber: "+254705577633"
_id: "ZPWNsQ7TWSJcERSJv"
...and leaves out:
payersName: "The LucidOreo"
payersUserId: "KDJFGkCRF3E5nY37e"
recipientNumber: "+254705086633"
_id: "MMyinrcrrYpz2haxq"
...which clearly carries the recipientNumber: "+254705086633"
the logged in users number.
To clarify, the desired yield would be:
0:
payersName: "The LucidOreo"
payersUserId: "KDJFGkCRF3E5nY37e"
recipientNumber: "+254705086633"
_id: "MMyinrcrrYpz2haxq"
1:
payersName: "Sir.BT"
payersUserId: "sxY3fsY9LmJYqFkiG"
recipientNumber: "+254705086633"
_id: "YbPFT9ecCA4FTgqsn"
2:
payersName: "Sir.BT"
payersUserId: "sxY3fsY9LmJYqFkiG"
recipientNumber: "+254705577633"
_id: "ZPWNsQ7TWSJcERSJv"
Can any one kindly explain to me why this is left out and how to better formulate the query in subject?
I believe it should work if you replace the following:
recipientsDetails.find( { $and: [{payersUserId: user}, {recipientNumber: usersNumber } ] } ).fetch();
with
recipientsDetails.find( { $or: [{payersUserId: user}, {recipientNumber: usersNumber } ] } ).fetch();