Vikram Biwal
Vikram Biwal

Reputation: 2826

How to use this aggregate condition in golang mongodb

Ho can i write this condition in golang for mongodb:

success_count: { $sum: { $cond: ["$is_success", 1, 0] } }

I a'm trying this but not working:

"$success_count": bson.M{"$sum": bson.M{"$cond": bson.M{"$is_success", 1, 0}}}

Upvotes: 0

Views: 1052

Answers (2)

Breno Teodoro
Breno Teodoro

Reputation: 483

You can also use a bson.A, which is just a representation of an Array.

bson.M{"$cond": bson.A{"$is_success", 1, 0}}

Upvotes: 0

Eklavya
Eklavya

Reputation: 18410

Use []interface{} instead of bson.M. Here bson.M is map[string]interface{} but you need to use slice.

bson.M{"$cond": []interface{}{"$is_success", 1, 0}}

Upvotes: 1

Related Questions