I am looking for convinient way to check if object already exists in collection. For now the only way that i have found is
type result interface{}
var res result
err := col.Find(bson.M{"title": "title1"}).One(&res)
if err != nil {
if err.Error() == "not found" {
log.Println("No such document")
} else {
log.Println("err occured", err)
}
}
I dont want to create variable res, in case if object exists, it can be very heavy document with a lot of fields. I wish there would be another way, some Check() function which will just return bool value.. Basically I only need to know that object already stored in collection, I dont need itself
count, err = collection.Find(bson.M{field: value}).Count()
you have to use $exists
Syntax: { field: { $exists: } }
For more details
http://docs.mongodb.org/manual/reference/operator/query/exists/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With