Translate

Friday, May 3, 2013

Search in MongoDB

I came across this incidents while working on some MongoDB. I am not sure it is the expected behavior, so thought of posting it.

I have a collection called zipcodes which has following attributes.

image

So, I need to get the count for two state , I executed following script.

image

it returned me  1596 which was incorrect. Then started to go more into details, I started with by interchanging the values and then I got a different results.

image

Then I remove first row and executed it again.

image

Oops, I got the same values as before. Then I realize that, whenever we pass values for same attribute only the last one will be taken in. So for the above case I should us $in.

This behavior is same for insert, update, find etc.

1 comment:

  1. That's expected behaviour, count takes a query document and documents have unique keys.

    If you create a document with duplicate keys, the last value will win:

    > x = {a: 1, a: 2}
    { "a" : 2 }

    ReplyDelete