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: "" }, 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!

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…


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

Giving then the console result(y) of:

{ link: '' }
{ link: '' }

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