Re: [mongodb-user] query mongodb java, help with a complex query

802 views
Skip to first unread message

Michael Dirolf

unread,
May 19, 2010, 2:46:24 PM5/19/10
to mongod...@googlegroups.com
I think you're looking for $elemMatch:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ConditionalOperator%3A%24elemMatch

On Wed, May 19, 2010 at 2:39 PM, matt <mathia...@gmail.com> wrote:
> hi guys,
> I have the json documents that follow this example:
>
> "container" : {
>    "attributes" : {
>        "attribute" : [
>                { "def" : { "elements" : {
>                        "element" : [ { "value" : "a"} , { "value" : "b"} , { "value" :
> "c"} , { "value" : "d"}]} ,
>                        "defaultValue" : { "value" : "a"}} ,
>                        "name" : "att1"
>                } ,
>                { "def" : {
>                        "upperBound" : {"value" : "10"} ,
>                        "lowerBound" : {"value" : "5"} ,
>                        "initValue" : {"value" : "5"}} ,
>                        "name" : "att2"
>                } ,
>                { "def" : {
>                        "upperBound" : {"value" : "20"} ,
>                        "lowerBound" : {"value" : "15"} ,
>                        "initValue" : {"value" : "15"}} ,
>                        "name" : "att3"
>                }
>        ]
>   }
> }
> my objective is to do a query to retrieve a "container" by querying
> its attributes based on an attribute name and whether or not it
> matches a particular value.
> if I use this query:
>
> BasicDBObject query = new BasicDBObject();
> query.put("container.attributes.attribute.def.name","att3");
> query.put("container.attributes.attribute.def.elements.element.value","a");
>
> to retrieve a container that has an attribute named att3 and that has
> a value of "a"
>
> I understand the query is incorrect because the first part of the
> query is not linked to the second part of the query. here name=att3
> matches but att3 doesn't have elements.element.value"="a", this gets
> matched with att1.
>
> in an xpath query I would write something like this; container/
> attributes/attribute/def[name="att3" and elements/element/value =
> "a"]. How can I do this with Mongo DB?
>
> thanks,
>
>
>
>
> --
> 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.
>
>

--
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.

matt

unread,
May 19, 2010, 2:39:42 PM5/19/10
to mongodb-user

matt

unread,
May 19, 2010, 3:23:32 PM5/19/10
to mongodb-user
Michael,
i'm a new user. any guidance on how to do this using the java driver?
i've been trying but failed.

thanks.

mathias

On May 19, 11:46 am, Michael Dirolf <m...@10gen.com> wrote:
> I think you're looking for $elemMatch:http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-...
> > For more options, visit this group athttp://groups.google.com/group/mongodb-user?hl=en.
>
> --
> 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 athttp://groups.google.com/group/mongodb-user?hl=en.

Michael Dirolf

unread,
May 19, 2010, 4:03:36 PM5/19/10
to mongod...@googlegroups.com
Haven't tested this but should look something like:

BasicDBObject elemMatch = new BasicDBObject();
elemMatch.put("name","att3");
elemMatch.put("def.elements.element.value","a");
BasicDBObject foo = new BasicDBObject();
foo.put("$elemMatch", elemMatch);
BasicDBObject query = new BasicDBObject();
query.put("container.attributes.attribute", foo);

Basically just build up a query that looks the same as the JS examples
do - it's a little bit more cumbersome to do w/ the Java API though.

matt

unread,
May 19, 2010, 4:11:21 PM5/19/10
to mongodb-user
thanks. I tried this and i'm getting a runtime exception has follows:

the generated query is; { "containers.attributes.attribute" :
{ "$elemMatch" : { "name" : "att3" , "def.elements.element.value" :
"a"}}}
the exception is:

java.lang.RuntimeException: db error [invalid operator: $elemMatch]
at com.mongodb.DBApiLayer$MyCollection.find(DBApiLayer.java:307)
at com.mongodb.DBCursor._check(DBCursor.java:253)
at com.mongodb.DBCursor._hasNext(DBCursor.java:374)
at com.mongodb.DBCursor.hasNext(DBCursor.java:399)
at com.hp.esit.test.JacksonTest.regexpMatchQuery(JacksonTest.java:
180)
at com.hp.esit.test.JacksonTest.testJackson(JacksonTest.java:220)
at com.hp.esit.test.JacksonTest.main(JacksonTest.java:231)

i'm running the 1.4 driver. should i migrate to 2.0rc? any thoughts on
what the problem is?

thanks for helping me out.

mathias

Michael Dirolf

unread,
May 19, 2010, 4:13:52 PM5/19/10
to mongod...@googlegroups.com
What version of the database are you running? $elemMatch requires >= 1.3.1

matt

unread,
May 19, 2010, 5:18:35 PM5/19/10
to mongodb-user
I see, i'm running mongodb-win32-i386-1.2.4. Just upgraded on 1.4.2
and i'm not getting the exception anymore and it works.
thanks very much for the help, really appreciated!

mathias
Reply all
Reply to author
Forward
0 new messages