From 64728c28a4b43045da7920ad8e43a7dc88367be4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Nieto?= <xiam@menteslibres.org>
Date: Fri, 19 Jul 2013 15:51:57 +0000
Subject: [PATCH] Fixing db.Fields for MongoDB, related:
 https://groups.google.com/forum/?fromgroups=#!topic/gosexy/3QcW40qZYfk

---
 mongo/collection.go | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/mongo/collection.go b/mongo/collection.go
index 369e6bab..cbf72369 100644
--- a/mongo/collection.go
+++ b/mongo/collection.go
@@ -289,11 +289,13 @@ func (self *SourceCollection) buildQuery(terms ...interface{}) *mgo.Query {
 	var delim = struct {
 		Limit  int
 		Offset int
+		Fields *db.Fields
 		Sort   *db.Sort
 	}{
 		-1,
 		-1,
 		nil,
+		nil,
 	}
 
 	// Conditions
@@ -301,6 +303,8 @@ func (self *SourceCollection) buildQuery(terms ...interface{}) *mgo.Query {
 
 	for i, _ := range terms {
 		switch t := terms[i].(type) {
+		case db.Fields:
+			delim.Fields = &t
 		case db.Limit:
 			delim.Limit = int(t)
 		case db.Offset:
@@ -322,7 +326,16 @@ func (self *SourceCollection) buildQuery(terms ...interface{}) *mgo.Query {
 		res = res.Limit(delim.Limit)
 	}
 
-	// Sorting result
+	// Delimiting fields.
+	if delim.Fields != nil {
+		sel := bson.M{}
+		for _, field := range *delim.Fields {
+			sel[field] = true
+		}
+		res = res.Select(sel)
+	}
+
+	// Sorting result.
 	if delim.Sort != nil {
 		for key, val := range *delim.Sort {
 			sval := to.String(val)
-- 
GitLab