forked from ungerik/go-start
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongoiterator.go
44 lines (38 loc) · 1.13 KB
/
mongoiterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package mongo
import (
"github.com/ungerik/go-start/model"
"labix.org/v2/mgo"
)
///////////////////////////////////////////////////////////////////////////////
// MongoIterator
func NewMongoIterator(query Query) model.Iterator {
mgoQuery, err := query.mongoQuery()
if err != nil {
return model.NewErrorOnlyIterator(err)
}
return &MongoIterator{collection: query.Collection(), iter: mgoQuery.Iter()}
// mgoIter := mgoQuery.Iter()
// collection, selectors := collectionAndSubDocumentSelectors(query)
// return &MongoIterator{collection: collection, selectors: selectors, iter: mgoIter}
}
type MongoIterator struct {
collection *Collection
// selectors []string
iter *mgo.Iter
}
func (self *MongoIterator) Next(resultRef interface{}) bool {
if self.iter.Err() != nil {
return false
}
if !self.iter.Next(resultRef) {
return false
}
// resultRef has to be initialized again,
// because mgo zeros the struct while unmarshalling.
// Newly created slice elements need to be initialized too
self.collection.InitDocument(documentFromResultPtr(resultRef))
return true
}
func (self *MongoIterator) Err() error {
return self.iter.Err()
}