Issue in Filter operation in Aggregation pipeline

I’m using the Go driver go.mongodb.org/mongo-driver v1.3.4
The data is stored as:

{    "_id":

{"$oid":"5e7b22bd2a7912a5a3b73d79"},
   "manufacturerID":"19193",
   "units":[
                  {
                      "assetsReserved":{
                                "departmentCode" : "PHY_DEPTS",
                                "assets":{"primaryID":"1234"}}
                  },
                 {
                          "departmentCode" : "PHY_DEPTS",
                          "assetsReserved":{"assets":{"primaryID":"4567"}}
                 }
             ]
}

The requirement is to get all the units where primaryID matches the “1234”

The mongo.Pipeline is made up of

matchStage := bson.D{primitive.E{Key: "$match", Value: bson.D{primitive.E{Key: "manufacturerID", Value: "19193"}}}}

projectStage := bson.D{
                                      {"$project", bson.D{
                                                              {"units", bson.D{
                                                                           {"$filter", bson.D

{                                                                            

{"input", "$units"},
{"as", "units"},
{"cond", bson.D{
                           {"$or", bson.A{                                                                                                      
               bson.D{{"$eq", bson.A{"$$units.assetsReserved.departmentCode", "1234"}}},
              bson.D{{"$eq", bson.A{"$$units.assetsReserved.assets.primaryID", "1234"}}},
}},
}},
}},
}},
}},
}

unwindStage := bson.D {{"$unwind", "$units"}}

The search based on
units.assetsReserved.departmentCode works

If we pass “PHY_DEPTS” we get the response, however

units.assetsReserved.assets.primaryID doesn’t return any response

If we pass “1234”

“$elemMatch” works fine with assetsReserved.assets.primaryID and is able to return result however it returns only 1 record even when multiple records with same primaryID exists which is the expected behavior based on documentation.
Looks like issues with $filter and cond that its not able to filter when subdocument contains an array

Hi @Abhay_Kumar, and welcome to the forum!

Using the example document above, and the code snippet that you provided I could return the result. The only different that I noticed here, is only the values specified on the $project stage, i.e.

projectStage := bson.D{
	{"$project", bson.D{
		{"units", bson.D{
			{"$filter", bson.D{
				{"input", "$units"},
				{"as", "units"},
				{"cond", bson.D{
					{"$or", bson.A{
						bson.D{{"$eq", bson.A{"$$units.assetsReserved.departmentCode", "PHY_DEPTS"}}},
						bson.D{{"$eq", bson.A{"$$units.assetsReserved.assets.primaryID", "1234"}}},
					}},
				}},
			}},
		}},
	}},
}

If you’re still experiencing an issue with this, could you provide the following to describe the problem better:

  • The result that you’re seeing
  • The desired result that you’re expecting
  • Extra document examples to demonstrate the issue better (if relevant)

Regards,
Wan.

Hi Wan,

Thank you for looking into this.

You are getting the result because the first or condition matched try the case where the first condition in the or doesn’t match and the second condition does match.

bson.D{{"$eq", bson.A{"$$units.assetsReserved.departmentCode", "XYZ_DEPTS"}}},
bson.D{{"$eq", bson.A{"$$units.assetsReserved.assets.primaryID", "1234"}}},

“$$units.assetsReserved.assets.primaryID”, “1234” doesn’t return result even if it satisfy the match condition.

Thank you.

Regards
Abhay Kumar

Hi @Abhay_Kumar,

The $or conditional statement works as expected in my test. I can change either of the conditions and the logical OR is still correct and return the result expected. Only if both conditions do not match that it returns an empty result.

If you still encountering this issue please provide:

  • MongoDB server version
  • Minimal reproducible code example

Best regards,
Wan