Check object existence in mongo using gopkg.in/mgo.v2

后端 未结 3 979
走了就别回头了
走了就别回头了 2021-01-12 04:16

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 re         


        
相关标签:
3条回答
  • 2021-01-12 04:52

    in the Official Mongo Driver you can get count of documents with specific key using CountDocuments function :

    count, err := collection.CountDocuments(context.TODO(), bson.D{{"key", "value"}})

    0 讨论(0)
  • 2021-01-12 05:01
    count, err = collection.Find(bson.M{field: value}).Count()
    
    0 讨论(0)
  • 2021-01-12 05:06

    you have to use $exists

    Syntax: { field: { $exists: } }

    For more details

    http://docs.mongodb.org/manual/reference/operator/query/exists/

    0 讨论(0)
提交回复
热议问题