From f689a157e63a5b2674d1930b8faed832018bee67 Mon Sep 17 00:00:00 2001 From: Iurii Panarin Date: Sun, 28 Jul 2019 23:14:07 +0200 Subject: [PATCH] feat(count): add count --- main.go | 1 + mongo/mongo.go | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/main.go b/main.go index e7da07f..2757eb7 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( type MongoDbConnector interface { Connect(mongoUrl string, databaseName string) error Disconnect() + Count(collectionName string, findPredicate bson.M) (int, error) InsertOne(collectionName string, entity interface{}) error UpdateOne(collectionName string, findPredicate bson.M, updatePredicate bson.M) error UpdateAll(collectionName string, findPredicate bson.M, updatePredicate bson.M) (*mgo.ChangeInfo, error) diff --git a/mongo/mongo.go b/mongo/mongo.go index 439f006..f780eee 100644 --- a/mongo/mongo.go +++ b/mongo/mongo.go @@ -140,6 +140,13 @@ func (c *MongoConnector) Disconnect() { c.session.isConnected = false } +func (c *MongoConnector) Count(collectionName string, findPredicate bson.M) (int, error) { + sessionCopy := c.session.session.Copy() + defer sessionCopy.Close() + collection := sessionCopy.DB(c.session.dataBaseName).C(collectionName) + return collection.Find(findPredicate).Count() +} + func (c *MongoConnector) GetIterator(collectionName string, findPredicate bson.M, opts *MongoOptions) *MongoIterator { sessionCopy := c.session.session.Copy() collection := sessionCopy.DB(c.session.dataBaseName).C(collectionName)