Skip to content
Browse files

Count function

  • Loading branch information...
1 parent 43e24c1 commit 43caa585fbac06708c239b27d8d28831da0303d9 @mikejs committed Nov 21, 2009
Showing with 13 additions and 0 deletions.
  1. +10 −0 mongo.go
  2. +3 −0 mongo_test.go
View
10 mongo.go
@@ -262,6 +262,16 @@ func (coll *Collection) FindOne(query BSON) (BSON, os.Error) {
return cursor.GetNext();
}
+func (coll *Collection) Count(query BSON) (int64, os.Error) {
+ cmd := &_Object{map[string]BSON{"count": &_String{coll.name, _Null{}}, "query": query}, _Null{}};
+ reply, err := coll.db.Command(cmd);
+ if err != nil {
+ return -1, err
+ }
+
+ return int64(reply.Get("n").Number()), nil;
+}
+
func (coll *Collection) update(um *updateMsg) os.Error {
um.requestID = rand.Int31();
conn := coll.db.conn;
View
3 mongo_test.go
@@ -44,6 +44,9 @@ func TestStuff(t *testing.T) {
assertTrue(doc.Get("fourth").Kind() == mongo.ObjectKind, "returned doc has proper 'fourth' element", t);
assertTrue(doc.Get("fifth").Get("f").String() == "i" && doc.Get("fifth").Get("v").String() == "e", "returned doc has proper 'fifth' element", t);
+ count, err := coll.Count(q);
+ assertTrue(count == 1, "count", t);
+
newDoc, _ := mongo.Marshal(map[string]string{"first": "one", "second": "two", "third": "three"});
coll.Update(q, newDoc);
doc, _ = coll.FindOne(q);

0 comments on commit 43caa58

Please sign in to comment.
Something went wrong with that request. Please try again.