double foo = 1.0;
double bar = 2.0;
MongoCollection<BsonDocument> items = db.GetCollection<BsonDocument>("collection");
var query = Query.And(
(Query.GT("field_one", foo).LT(bar)),
(Query.GT("field_two", foo).LT(bar))
);
MongoCursor<BsonDocument> dbResult = items.Find(query)
.SetLimit(count);
Showing posts with label MongoDB. Show all posts
Showing posts with label MongoDB. Show all posts
Monday, June 13, 2011
MongoDB C# Driver, Querying multiple fields with intervals
Simple way of chaining together a query when looking for records that require both field one and field two to be in a certain interval.
Thursday, June 9, 2011
MongoDB C# Driver, Query multiple tags that are required
Tried hard to find the solution to querying multiple tags through the MongoDB C# driver.
Many of the solutions that I found were about using OR to put together the tags but I needed to AND the tags. I.e. all tags supplied were required to be present on in the result.
The solution was quite simple actually. Just my mind that thought it would require some special solution.
Many of the solutions that I found were about using OR to put together the tags but I needed to AND the tags. I.e. all tags supplied were required to be present on in the result.
The solution was quite simple actually. Just my mind that thought it would require some special solution.
string[] tags = new string[] {"foo", "bar"};
var sort = SortBy.Descending("created");
var query = MongoDB.Driver.Builders.Query.All("tags", BsonArray.Create(tags));
var dbPages = pages.Find(query)
.SetSortOrder(sort)
.SetLimit(limit)
.SetSkip(offset);
- Create a string array with the tags that you want to query with
- Add sorting if needed
- Call MongoDB.Driver.Builders.Query to build your query
- Use find on your MongoCollection together with your query.
- Add Limit and Offset if needed.
Subscribe to:
Posts (Atom)