go-bongo / bongo

Go ODM for MongoDB
MIT License
487 stars 40 forks source link

Best way to check if exist with some conditions #39

Closed renatocassino closed 5 years ago

renatocassino commented 5 years ago

Hello guys

I want to make an endpoint in my application to check if a slug already exists I made in this way:

connection := storage.DbBongo()
connection.Collection("mycollection").FindOne(bson.M{"slug": slug}, &post)

isAvailable := post.Id == ""
/// RESPONSE IF IS AVAILABLE AFTER

But, in this query to MongoDb I'm getting all post data I want to make a simple query getting only if exist or not

To simplify, I want to count

Like this:

isAvailable := connection.Collection("communities").Find(bson.M{"slug": slug}).Count() == 0

How can I do this?

Thanks