Rabin
unread,May 24, 2012, 6:18:36 PM5/24/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mongodb-csharp
I am fairly new to this mongodb c# driver.
Either I am doing terribly wrong or this $and api enhanced as part of
mongodb 1.4.2 has some bug in it.
Below is what I have in MongoDB in products collection:
/* 0 */
{
"_id" : 1,
"productTag" : [{
"id" : "title",
"value" : "Title"
}, {
"id" : "price",
"value" : 2301
}, {
"id" : "color",
"value" : ""
}],
"siteId" : "site1",
"pageId" : "page1"
}
/* 1 */
{
"_id" : 2,
"productTag" : [{
"id" : "title",
"value" : "Title"
}, {
"id" : "price",
"value" : 2302
}, {
"id" : "color",
"value" : "red"
}],
"siteId" : "site1",
"pageId" : "page1"
}
/* 2 */
{
"_id" : 3,
"productTag" : [{
"id" : "title",
"value" : "Title"
}, {
"id" : "price",
"value" : 2303
}, {
"id" : "color",
"value" : "yellow"
}],
"siteId" : "site1",
"pageId" : "page1"
}
Below is the c# code:
MongoCollection<BsonDocument> productCollection =
dbSource.GetCollection<BsonDocument>("products");
var query1 = Query.And(Query.EQ("id", "price"), Query.GTE("value",
2301).LTE(2302));
var query2 = Query.And(Query.EQ("id", "color"), Query.EQ("value",
"red"));
var dynQuery = Query.And(query1,query2);
var finalQuery = Query.ElemMatch("productTag", dynQuery);
foreach (BsonDocument product in productCollection.Find(finalQuery))
{
//fetch data here and mapipulate
}
When I debug and see finalQuery:
{ "productTag" : { "$elemMatch" : { "$and" : [{ "id" : "price" },
{ "value" : { "$gte" : 2301, "$lte" : 2302 } }, { "id" : "color" },
{ "value" : "red" }] } } }
and my guess is this query is not generated properly and that is why I
am not getting any result.
If I run just the query1 I get result and when I see finalQuery, its
generated as below, and there's a slight difference, there's no close
brace after "price" instead one extra close brace is added in the end:
{ "productTag" : { "$elemMatch" : { "id" : "price", "value" :
{ "$gte" : 2301, "$lte" : 2302 } } } }
Please help me solve this issue.
If I am not using the code properly then please help me correct it so
that I can start getting result and get back to my happy life again.
Thank you so much!!!
Rabin