gte/lte range queries with the official c# client

612 views
Skip to first unread message

a.

unread,
Dec 20, 2010, 7:02:20 PM12/20/10
to mongodb-user
Hi,

Does the Query class support these kind of queries?

{ field: { $gte: lowerBound, $lte: upperBound } }

Using Query.And to combine the two parts throws an
InvalidOperationException in BsonDocument.

Query.And(Query.GTE(fieldName, lowerBound), Query.LTE(fieldName,
upperBound));

The only way i was able to build a query is something like:

Query.EQ(fieldName, new BsonDocument { Query.EQ("$gte",
lowerBound).ToBsonDocument(), Query.EQ("$lte",
upperBound).ToBsonDocument() })

Of course manually building the whole query with BsonDocuemnt works
too, but i'd think there is an easier way to do this.

If this is unsupported, please let me know, and i'll try to implement
it for myself.

Thanks,



a.

Robert Stam

unread,
Dec 20, 2010, 7:37:33 PM12/20/10
to mongodb-user
The way to do this is:

var query = Query.GTE("field", lowerBound).LTE(upperBound);

The structure of this query mirrors the structure of the BsonDocument
equivalent.

You're not the first to ask this. I'm beginning to think Query.And
should work as you thought it should and rewrite the underlying query
as required.

a.

unread,
Dec 20, 2010, 7:44:42 PM12/20/10
to mongodb-user
It wasn't quite obvious that if I chain the methods it will do what i
want so i did not even try...

Maybe adding a samples page or some xml docs would make wonders :]

Thanks for the help.


a.

Stuart Johnson

unread,
Dec 21, 2010, 5:47:32 AM12/21/10
to mongod...@googlegroups.com
This confused me for a while, but I think it's great as it is. The
10gen driver keep surprising me with things that have been so well
thought out.
Reply all
Reply to author
Forward
0 new messages