c# - Using an array in a MongoDB aggregation query with $or -
i'm trying use array in mongodb c# aggregation query $or statement. code follows:
var groupsarray = new bsonarray {new bsondocument { { "role", 1 } }, new bsondocument { { "role", 2 } }, new bsondocument { { "role", 3 } } }; var bsonor = new bsondocument { { "$or", groupsarray } }; var match = new bsondocument { { "$match", new bsondocument { { "$or", bsonor } } } };
however, i'm getting following exception:
mongodb.driver.mongocommandexception: command 'aggregate' failed: exception: bad query: badvalue $or needs array (response: { "errmsg" : "exception: bad query: badvalue $or needs array", "code" : 16810, "ok" : 0.0 }).
is there specific way use array in query i'm missing?
the or
being added twice. code changed
var groupsarray = new bsonarray(); foreach (var g in groups) { groupsarray.add(new bsondocument() { { "role", g.id.tostring() } }); } var bsonor = new bsondocument { { "$or", groupsarray } }; var match = new bsondocument { { "$match", bsonor } };
and worked hoped.
Comments
Post a Comment