An object representing an expression must have exactly one field: { $ifNull: [ "$num", null ], then: "$name.name", else: "$con" }

on stage $addFields got error

(Location15983) Invalid $addFields :: caused by :: An object representing an expression must have exactly one field: { $ifNull: [ "$num", null ], then: "$name.name", else: "$con" }
(Location16410) Invalid $addFields :: caused by :: FieldPath field names may not start with '$'. Consider using $getField or $setField.

Aggregate in mongo compass

[{
    {
    $lookup: {
        from: 'contact',
        'let': {
            contact: '$num',
            user_id: '$u_id'
        },
        pipeline: [{
                $match: {
                    $expr: {
                        $eq: [
                            '$u_id',
                            '$$user_id'
                        ]
                    }
                }
            },
            {
                $unwind: {
                    path: '$phone',
                    preserveNullAndEmptyArrays: true
                }
            },
            {
                $match: {
                    $expr: {
                        $eq: [
                            '$phone',
                            '$$contact'
                        ]
                    }
                }
            },
            {
                $limit: 1
            },
            {
                $project: {
                    name: 1,
                    _id: 0
                }
            }
        ],
        as: 'name'
    }
}, {
    $addFields: {
        name: {
            $arrayElemAt: [
                '$name',
                0
            ]
        }
    }
}, {
    $addFields: {
        con: {
            $cond: {
                'if': {
                    $ifNull: [
                        '$num',
                        false
                    ]
                },
                then: '$name.name',
                'else': '$con'
            }
        }
    }
}, {
    $project: {
        name: 0
    }
}]

Aggregate in golang got error

func getContactByNumberAggregate() mongo.Pipeline {
	return mongo.Pipeline{
		{{
			"$lookup", bson.M{
				"from": configs.CONTACT_COLLECTION,
				"let":  bson.M{"contact": "$num", "user_id": "$u_id"},
				"pipeline": bson.A{
					bson.M{
						"$match": bson.M{
							"$expr": bson.M{
								"$eq": bson.A{
									"$u_id", "$$user_id"},
							},
						},
					},
					bson.M{
						"$unwind": bson.M{
							"path":                       "$phone",
							"preserveNullAndEmptyArrays": true,
						},
					},
					bson.M{
						"$match": bson.M{
							"$expr": bson.M{
								"$eq": bson.A{"$phone", "$$contact"},
							},
						},
					},
					bson.M{
						"$limit": 1,
					},
					bson.M{
						"$project": bson.M{
							"name": 1,
							"_id":  0,
						},
					},
				},
				"as": "name",
			},
		}},

		{{
			"$addFields", bson.M{
				"name": bson.M{"$arrayElemAt": bson.A{"$name", 0}},
			},
		}},

		{{
			"$addFields", bson.M{
				"con": bson.M{
					"$cond": bson.M{
						"if": bson.M{
							"$ifNull": bson.A{"$num", nil},
							"then":    "$name.name",
							"else":    "$con",
						}}},
			},
		}},

		{{
			"$project", bson.M{
				"name": 0,
			},
		}},
	}
}

In JS, your if:, then: and else: fields are all direct fields of your $cond object.

In golang, the then: and else: are fields of the if:. I would move one of the closing brace from the line where you have 3 to the line after $ifNull to close the if:. This way then: and else: will be fields of $cond.

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