Skip to content

Commit 74ddeac

Browse files
bozarodomodwyer
authored andcommitted
Add Min/Max find options (#201)
* Add Min/Max find options * Add simple test for Min/Max find methods
1 parent 09adb3b commit 74ddeac

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

session.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3455,6 +3455,34 @@ func (q *Query) Sort(fields ...string) *Query {
34553455
return q
34563456
}
34573457

3458+
// Specify a $min value to specify the inclusive lower bound for a specific index in order to constrain the results of
3459+
// Find(). The $min specifies the lower bound for all keys of a specific index in order.
3460+
//
3461+
// Relevant documentation:
3462+
//
3463+
// https://docs.mongodb.com/manual/reference/operator/meta/min/
3464+
//
3465+
func (q *Query) Min(min interface{}) *Query {
3466+
q.m.Lock()
3467+
q.op.options.Min = min
3468+
q.m.Unlock()
3469+
return q
3470+
}
3471+
3472+
// Specify a $max value to specify the exclusive upper bound for a specific index in order to constrain the results of
3473+
// Find(). The $max specifies the upper bound for all keys of a specific index in order.
3474+
//
3475+
// Relevant documentation:
3476+
//
3477+
// https://docs.mongodb.com/manual/reference/operator/meta/max/
3478+
//
3479+
func (q *Query) Max(max interface{}) *Query {
3480+
q.m.Lock()
3481+
q.op.options.Max = max
3482+
q.m.Unlock()
3483+
return q
3484+
}
3485+
34583486
// Collation allows to specify language-specific rules for string comparison,
34593487
// such as rules for lettercase and accent marks.
34603488
// When specifying collation, the locale field is mandatory; all other collation
@@ -3787,6 +3815,8 @@ func prepareFindOp(socket *mongoSocket, op *queryOp, limit int32) bool {
37873815
MaxTimeMS: op.options.MaxTimeMS,
37883816
MaxScan: op.options.MaxScan,
37893817
Hint: op.options.Hint,
3818+
Min: op.options.Min,
3819+
Max: op.options.Max,
37903820
Comment: op.options.Comment,
37913821
Snapshot: op.options.Snapshot,
37923822
Collation: op.options.Collation,

session_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,37 @@ func (s *S) TestViewWithCollation(c *C) {
16341634
c.Assert(docs[1].Nm, Equals, "CaSe")
16351635
}
16361636

1637+
func (s *S) TestFindWithMinMax(c *C) {
1638+
if !s.versionAtLeast(3, 4) {
1639+
c.Skip("depends on mongodb 3.4+")
1640+
}
1641+
// CreateView has to be run against mongos
1642+
session, err := mgo.Dial("localhost:40001")
1643+
c.Assert(err, IsNil)
1644+
defer session.Close()
1645+
1646+
db := session.DB("mydb")
1647+
1648+
coll := db.C("mycoll")
1649+
1650+
for i := 0; i < 4; i++ {
1651+
err = coll.Insert(bson.M{"_id": i, "nm": "a"})
1652+
c.Assert(err, IsNil)
1653+
err = coll.Insert(bson.M{"_id": fmt.Sprintf("x%d", i), "nm": "b"})
1654+
c.Assert(err, IsNil)
1655+
}
1656+
1657+
ids := []interface{}{}
1658+
iter := coll.Find(nil).Sort("_id").Min(bson.M{"_id": 2}).Max(bson.M{"_id": "x2"}).Iter()
1659+
var doc bson.M
1660+
for iter.Next(&doc) {
1661+
ids = append(ids, doc["_id"])
1662+
}
1663+
c.Assert(iter.Err(), IsNil)
1664+
1665+
c.Assert(ids, DeepEquals, []interface{}{2, 3, "x0", "x1"})
1666+
}
1667+
16371668
func (s *S) TestCountQuery(c *C) {
16381669
session, err := mgo.Dial("localhost:40001")
16391670
c.Assert(err, IsNil)

socket.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ type queryWrapper struct {
8989
Query interface{} `bson:"$query"`
9090
OrderBy interface{} `bson:"$orderby,omitempty"`
9191
Hint interface{} `bson:"$hint,omitempty"`
92+
Min interface{} `bson:"$min,omitempty"`
93+
Max interface{} `bson:"$max,omitempty"`
9294
Explain bool `bson:"$explain,omitempty"`
9395
Snapshot bool `bson:"$snapshot,omitempty"`
9496
ReadPreference bson.D `bson:"$readPreference,omitempty"`

0 commit comments

Comments
 (0)