Re: Getting "Unsupported projection option: $elemMatch" when executing query from Java Mongo driver, but runs fine using mongo shell.

3,742 views
Skip to first unread message

Dwight Merriman

unread,
Dec 5, 2012, 8:59:58 PM12/5/12
to mongod...@googlegroups.com
i think the problme is part of your query is in the projection document field.  that's the second parameter to find() in the shell.  the shell isnt' complaining, but it probably is just evaluating 

 { "tokenList" : {$elemMatch : {"name" : "BRAK"}}}
as
 { "tokenList" : true }

and thus thinking you want that field (only) back.

so this works for me in the shell (slightly different data i entered):
> db.foo.find({ "_id" : 99 , "tokenList" : {$elemMatch : {"name" : "DRAK2"}}}).pretty()
{
"_id" : 99,
"_class" : "xyz",
"tokenList" : [
{
"_id" : 3,
"description" : "Sample token 2",
"name" : "DRAK",
"parentCategoryId" : "abc"
},
{
"_id" : 4,
"description" : "Sample token2",
"name" : "DRAK2",
"parentCategoryId" : "abc"
}
],
"name" : "test"
}

P.S. if elemMatch is just one field you can dispense with it and just do the simpler:
> db.foo.find({ "_id" : 99 , "tokenList.name" : "DRAK2"}}).pretty()


On Wednesday, December 5, 2012 2:35:22 PM UTC-8, shanti kadiyala wrote:

I am using Spring Data and Mongo Driver 2.10.0 .
 
Here is the data , I was querying on
 
{ "_id": { "$oid" : "50bfc7218df4decc94d2815d" },
"_class": "xyz",
"tokenList": [ { "_id": asdfhfg,
                      "description": "Sample token 2",
                      "name": "DRAK",
                      "parentCategoryId": "abc" },
               { "_id": jdsfhdsjf,
                       "description": "Sample token2",
                       "name": "DRAK2",
                        "parentCategoryId": "abc"
                }
              ],
 "name": "test"
}
 
Here is how the query was defined using Spring Data, to retrieve a specific sub-document from this array "tokenList".
 
@Query(value = "{ '_id' : ?0 }", fields = "{'tokenList' : {$elemMatch : {'name' : ?1}}}")
List<Token> getToken(String id, String tokenName);

I am getting the error "com.mongodb.MongoException: Unsupported projection option: $elemMatch" from the Monogo driver,
and after debugging further, the actual error message that is being returned  from Mongo is { "$err" : "Unsupported projection option: $elemMatch" , "code" : 13097} to the Java driver.

But If I run the same query using Mongo SHell, it runs fine with the expected result. i.e.

db.token.find({ "_id" : ObjectId("50b7330279e881b8a010c84e")}, { "tokenList" : {$elemMatch : {"name" : "BRAK"}}})

returns , the sub-document from the list and the _id of the top level document.

{ "_id": asdfhfg,
                      "description": "Sample token 2",
                      "name": "DRAK",
                      "parentCategoryId": "abc" },

Any insight into what I may be doing wrong would be quite helpful.

Thanks

shanti kadiyala

unread,
Dec 6, 2012, 12:07:04 PM12/6/12
to mongod...@googlegroups.com
Hi,
Thanks for the reply. I tried your suggestion, but it returns the whole document. What I wanted to do, using projection is to just get the sub-document, containing the name "DRAK".

If I run the query I mentioned, I do get just the sub-document from shell. But from Java driver, I am getting the exception.


db.poc.find({"_id" :99}, {"tokenList" : {$elemMatch : {"name" : "DRAK"}}}).pretty()

{
        "_id" : 99,
        "tokenList" : [
                {
                        "_id" : 3,
                        "description" : "Sample token 2",
                        "name" : "DRAK",
                        "parentCategoryId" : "abc"
                }
        ]
}

shanti kadiyala

unread,
Dec 6, 2012, 10:56:26 PM12/6/12
to mongod...@googlegroups.com
HI,
update: 

I have two mongoDB servers, one as master and another as slave.
It turns out that setting slave mongoDB as a read from instance,  is causing this issue.. (again need to see why)... If I turn the configuration off, and let read and write  happen from master, this query runs fine from java driver!!!

thanks
Reply all
Reply to author
Forward
0 new messages