How to query array of specific entry(document) in collection?

Hi everyone, I have been trying to do something for the past 48 hours, with not much luck…

I’m trying to query an array within a specific document, say "userA".friends, the .friends field is an array with a list of friends simple as { link: "userB@gmail.com" }, and I’m trying to find one within array, if exists already do one thing, if not does another, such as add the friend or remove the request field pertaining the user to be accepted…

But I haven’t been able to succesfully query this array, I was wondering if there is something such as db.collection.findOne({email: userlogged }, {"friends": {$in: { [usertoaccept] }}});

Does anybody know how to proceed?

Thank you!

EDIT:
I got as far as this… When querying the array result, trying to compare the result of friend[i] even with a match its coming back with no result…

code:

users.findOne({email: userlogged}).then(result =>{
	friendz = result.friends;
	if (friendz){
		users.findOne({ email: userlogged},{$in: { "friends": { $in: [usertoaccept, userid] }}}).then(resulty =>{
			console.log(resulty);
			console.log("Ressssabove");
			friendzz = resulty.friends[0];
			
			thatlogic = "{ link:" + " '" + usertoaccept + "' " + "}";
			
			console.log(thatlogic);
			console.log(friendzz);
			
			for (let i = 0; i < resulty.friends.length; i++){
				if(resulty.friends[i] === resulty.friends[i].link.thatlogic){
						console.log("Resulty found");
					}else{
						console.log("No resulty found.");
					}
				}
		})
	}
})

Giving then the console result(y) of:

{ link: 'userA@gmail.com' }
{ link: 'userA@gmail.com' }

Still… “No resulty found.” :frowning:

This is basic JS logic. You are using ===, it means that type and value must match.

The variable thatlogic is constructed as a string.

The variable friendzz is object return by findOne().

I am not too savvy in JS, but I am not sure if

is really equal to

1 Like