I am trying to fetch the details from the MongoDB where value of a particular attribute is either a or b.
For example,
In my collection Test(Attr_A, Attr_B, Attr_C) get the record where value of Attr_C is either "x" or "y".
Filter Query:
filter := bson.M{"Attr_A": "123", "Attr_B": "456",
"$in": bson.A{bson.D{{"Attr_C", "x"}},
bson.D{{"Attr_C", "y"}}}}
result := repo.DBPlug.Get("Test", filter)
Error I am getting is,
(BadValue) unknown top level operator: $in. If you have a field name that starts with a ‘$’ symbol, consider using $getField or $setField.
2
Answers
You should specify the field name for the
$in
operation.Since the JSON filter query should look like:
I guess in BSON it should be something like:
or something like:
or:
Since this is a filter, the use of bson.M or bson.D is according to your choice – there is no difference in this case. See elaboration
In pure JSON format your filter query would be:
Simply translating this to bson, you get