Is it possible to remove records where the lookup output field is empty without using unwind

Hi all,
my question is exactly as the title:
is it possible to remove records where the lookup output field is empty without using unwind

my current code: (golang)

pipeline := mongo.Pipeline{
	{
		{"$lookup", bson.D{
			{"from", "follows"},
			{"let", bson.D{
				{"followed", "$user._id"}},
			},
			{"pipeline", bson.A{bson.D{
				{"$match", bson.D{
					{"$expr", bson.D{
						{"$and", bson.A{
							bson.D{
								{"$eq", bson.A{"$followed", "$$followed"}},
							},
							bson.D{
								{"$eq", bson.A{"$follower", userID}},
							},
						}},
					}},
				}},
			}}},
			{"as", "helper"},
		}},
	},
	{
		{"$unwind", bson.D{
			{"path", "$helper"},
			{"preserveNullAndEmptyArrays", false},
		}},
	},
	{
		{"$unset", "helper"},
	},
}

this works fine, the lookup output field is helper
first unwind the field, and remove records where that field is empty (how can we do that but without using unwind at the first place)
and then we unset the field because i don’t need it (not related to the question)

ps: if you are interested what is the code for, it’s to get posts from users that i’m following, the helper is only used to get such data that’s why i unset it at the end.

You can use a match,to filter out the empty arrays(size=0).
But i dont think that match and unwind after will have any difference in perfomance.

false is the default,so you dont need this option

{"preserveNullAndEmptyArrays", false}

If the question is if you can remove the documents that didnt joined with any, at $lookup stage
i think its not possible.Because $lookup is left outer join

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.