I'm trying to do a query in mongo that basically will be...Get all documents that match this instance Id AND where the status does not equal Deleted or Rejected. I figured out how to do this in mongodb query but I'm having an issue translating it to golang mgo.
This is the mongodb query:
db.getCollection('instance_documents').find( {"$and":[
{ "status": {"$nin":['DELETED', "REJECTED"] }},
{"_id": “instanceID”}
]
})
This is what I've tried so far in golang, the query does not work properly, it returns nothing:
err := appInstanceCollection.Find(bson.M{
"$and": []bson.M{
{"status": bson.M{"$nin": []string{"REJECTED", "DELETED"}}},
{"_id": instanceID},
},
}).One(&instance)
$and
is unnecessary here. Mongo requires all fields in a query to be true for a document to be returned in the result set. Try setting them all in the same bson.M:
err := appInstanceCollection.Find(bson.M{
"status": bson.M{"$nin": []string{"REJECTED", "DELETED"}},
"_id": instanceID,
},
}).One(&instance)
As for why your provided $and
mgo query doesn't work, I'm not sure. It looks fine to me.