MongoDB 1.4.2 $and BUG

15 views
Skip to first unread message

Rabin

unread,
May 24, 2012, 6:18:36 PM5/24/12
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

Robert Stam

unread,
May 24, 2012, 6:25:32 PM5/24/12
to mongodb...@googlegroups.com
The way you have written your query you are trying to find a productTag whose "id" is simultaneously equal to both "price" and "color", which can never be true. 

Your best bet is to get a query working in the mongo shell first where it's easy to experiment and then translate that to C#.

Rabin

unread,
May 24, 2012, 6:33:33 PM5/24/12
to mongodb-csharp
Thank you for your response.
Looks like my code is wrong again and not the API.

Anyway, how do you understand this Query.And()
may be I am not understanding properly.

Thanks
Rabin

Robert Stam

unread,
May 24, 2012, 6:41:29 PM5/24/12
to mongodb...@googlegroups.com
How about you explain in words what you are trying to accomplish and I'll try to help you express it as a MongoDB query?

Rabin

unread,
May 24, 2012, 6:54:14 PM5/24/12
to mongodb-csharp
Using the above data structure in mongodb:
I am trying to get all productTags
whose price is in the range of 2301 and 2302
and color is red

Thank you for your help again.
I am testing on my end as well, running some query on mongo shell.

Rabin

Robert Stam

unread,
May 24, 2012, 7:48:49 PM5/24/12
to mongodb...@googlegroups.com
Based on the description of your query I think you need something like this:

    var query =
        Query.And(
            Query.ElemMatch("productTag", Query.And(Query.EQ("id", "price"), Query.GTE("value", 2301), Query.LTE("value", 2302))),
            Query.ElemMatch("productTag", Query.And(Query.EQ("id", "color"), Query.EQ("value", "red"))));

Rabin

unread,
May 24, 2012, 8:08:13 PM5/24/12
to mongodb-csharp
Thats it, you got it in one shot.
I have been struggling for couple hours. Basically I was not
understanding to use this Query.And properly.

Thank you so much for your help
Rabin
Reply all
Reply to author
Forward
0 new messages