Java: Bug in advanced queries

87 views
Skip to first unread message

shiraz

unread,
Nov 3, 2011, 8:19:56 AM11/3/11
to mongodb-user
hi,

I cannot get the expected results while using the advanced queries
with $and and $not operators

There are 50 of such documents in the mongodb
{ "ServiceType" : "sms" , "ServiceHealthState" : "ok"}

and 50 of
{ "ServiceType" : "jms" , "ServiceHealthState" : "critical"}

The following $and query should return the 50 records:
{"$and":[{"ServiceType":"sms"},{"ServiceHealthState":"ok"}]}

The above query returns 0 results, however the expected result count
should be 50

Likewise for the $not query
{"$not":{"Service_Endpoint_HealthState":"ok"}}

returns 0 results, the required count is 50

Surprisingly $or queries behaves correctly.

System:
The mongodb version is 1.8.3
java driver version 2.6.5

Thanks in advance,
Shiraz

Marc

unread,
Nov 3, 2011, 11:51:30 AM11/3/11
to mongodb-user
$and is a (relatively) new feature, released in MongoDB version 2.0.
It is not available in 1.8.

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24and

In order for your query to work, you must upgrade.

If upgrading is not possible for you at this time, 'and' queries were
implicit in versions 1.8 and below.

> db.services.save({ "ServiceType" : "sms" , "ServiceHealthState" : "ok"} )
> db.services.save({ "ServiceType" : "jms" , "ServiceHealthState" : "critical"})
> db.services.find({"ServiceType":"sms", "ServiceHealthState":"ok"})
{ "_id" : ObjectId("4eb2b1e43cf8b03f69a2fa0c"), "ServiceType" : "sms",
"ServiceHealthState" : "ok" }

If you upgrade to the latest version of MongoDB, $and will work as
advertised:
> db.version()
2.0.1
> db.services.find()
{ "_id" : ObjectId("4eb2b1e43cf8b03f69a2fa0c"), "ServiceType" : "sms",
"ServiceHealthState" : "ok" }
{ "_id" : ObjectId("4eb2b1f13cf8b03f69a2fa0d"), "ServiceType" : "jms",
"ServiceHealthState" : "critical" }
> db.services.find({$and:[{"ServiceType":"sms"}, {"ServiceHealthState":"ok"}]})
{ "_id" : ObjectId("4eb2b1e43cf8b03f69a2fa0c"), "ServiceType" : "sms",
"ServiceHealthState" : "ok" }

The Mongo documentation on the $not operator states that, "The $not
meta operator can be used to negate the check performed by a standard
operator." There are some examples in the documentation of how the
$not operator is used.

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-Metaoperator%3A%7B%7B%24not%7D%7D

If you would like to return all all of the documents in which the key
"ServiceHealthState" is not equal to "ok", the "$ne" (not equal)
operator should be used.

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24ne

> db.services.find({"ServiceHealthState":{$ne:"ok"}})
{ "_id" : ObjectId("4eb2b1f13cf8b03f69a2fa0d"), "ServiceType" : "jms",
"ServiceHealthState" : "critical" }

Hopefully the above examples will do what you were hoping to
accomplish.

Good Luck!

لسٹ शिराज़

unread,
Nov 3, 2011, 12:07:08 PM11/3/11
to mongod...@googlegroups.com
Many Thanks Marc, I will then try to upgrade to 2.0.1 manually, as the latest available mongodb on the suse repository is 1.8.3 



Best regards,
Shiraz


--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.




--
Cheers,
Shiraz

Scott Hernandez

unread,
Nov 3, 2011, 12:13:23 PM11/3/11
to mongod...@googlegroups.com
For your query you do not need $and.

2011/11/3 لسٹ शिराज़ <shiraz...@googlemail.com>:

mdahlman

unread,
Nov 4, 2011, 2:15:24 AM11/4/11
to mongodb-user
Scott, it's obvious once you mentioned it. Filtering on multiple
fields does not require the use of $and, so it's fine in pre-2.0
MongoDB. I haven't used $and yet. I see why $and is needed with
multikeys. Are there cases where it's useful outside of multikeys?
Shiraz, I hope you don't mind: I used your idea in a sample report to
make sure that things worked the way I expected them to work. I posted
my report showing the multi-field filter here:http://
mdahlman.wordpress.com/2011/11/03/mongodb-multi-field-filter/
Regards,Matt
On Nov 3, 9:13 am, Scott Hernandez <scotthernan...@gmail.com> wrote:
> For your query you do not need $and.
>
> 2011/11/3 لسٹ शिराज़ <shiraz.li...@googlemail.com>:
>
>
>
>
>
>
>
> > Many Thanks Marc, I will then try to upgrade to 2.0.1 manually, as the
> > latest available mongodb on the suse repository is 1.8.3
> >http://software.opensuse.org/search?q=mongodb&baseproject=openSUSE%3A...
>
> > Best regards,
> > Shiraz
>
> > On Thu, Nov 3, 2011 at 4:51 PM, Marc <m...@10gen.com> wrote:
>
> >> $and is a (relatively) new feature, released in MongoDB version 2.0.
> >> It is not available in 1.8.
>
> >>http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-...
> >>http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-...
>
> >> If you would like to return all all of the documents in which the key
> >> "ServiceHealthState" is not equal to "ok", the "$ne" (not equal)
> >> operator should be used.
>
> >>http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-...

Marc

unread,
Nov 9, 2011, 1:43:37 PM11/9/11
to mongodb-user
There are many cases when $and is useful.  In a nutshell (from the
feature request) "Condition blocks are physically objects. Thus, we
cannot have the same property used twice, preventing the use of the
same construct/property multiple times in a query."
There are several examples of use cases for $and in the Jira
ticket: https://jira.mongodb.org/browse/SERVER-1089
Reply all
Reply to author
Forward
0 new messages