Querying in a array of String with Java driver

1,727 views
Skip to first unread message

The JMCNet Team

unread,
Feb 27, 2013, 6:35:39 AM2/27/13
to mongod...@googlegroups.com
Hello,

Here is a view of my DB :
> db.userGroup.find().pretty()
{
        "_id" : "512DEA59178020D87F668A2F",
        "adminAccountIds" : [
                "512DEA4F178020D87F668A2D"
        ]
}
{
        "_id" : "512DEA4F178020D87F668A2E",
        "adminAccountIds" : [
                "512DEA4F178020D87F668A2D",
                "512DEA4F178020D87F668A2G",
        ]
}

I want to query all userGroup that have a adminAccountIds equals to a parameter.

In console I've done this :
db.userGroup.find('{ "adminAccountIds" : { "$elemMatch" : "512DEA4F178020D87F668A2D"}}').pretty()
and it works.

I try to do the same programmatically with the Java driver :
DBCollection userGroup = db.getCollection("userGroup");
        BasicDBObject query = new BasicDBObject("adminAccountIds", new BasicDBObject("$elemMatch",
"512DEA4F178020D87F668A2D"));
        DBCursor qRes = userGroup.find(
                query, new BasicDBObject("_id", 1));


but the result is :
com.mongodb.MongoException: invalid parameter: expected an object ($elemMatch)
    at com.mongodb.MongoException.parse(MongoException.java:82)


Any idea would be greatly appreciated.

Thank's in advance.

Russell Bateman

unread,
Feb 27, 2013, 9:47:03 AM2/27/13
to mongod...@googlegroups.com
Here's a scrape of my DAO code. In POJO Account, field identities is defined thus:
public class AccoundDao
{
    ....
    private List< String > identities;
    ....
}
This returns an array of all the documents whose identities array contains at least the string identity.

      public List< Account > readAllByIdentity( String identity )
    {
        List< Account > list  = new ArrayList< Account >();
        BasicDBObject   query = new BasicDBObject();
        query.put( "identities", identity );
        query.put( "forgotten", false );
        DBCursor cursor = getCollection().find( query );

        while( cursor.hasNext() )
        {
            DBObject dbo = cursor.next();

            Account account = new Account();
            account.makePojoFromBson( dbo );

            list.add( account );
        }

        return list;
    }


Hope this helps.
--
--
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
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

The JMCNet Team

unread,
Feb 27, 2013, 10:17:35 AM2/27/13
to mongod...@googlegroups.com
It works perfectly but I don't understand why ... How MongoDB understand that this simple query : query.put( "identities", identity );
means "where identity in identities" ? Is there a special treatment MongoDB side for Array of String ?

JM.

Russell Bateman

unread,
Feb 27, 2013, 10:32:46 AM2/27/13
to mongod...@googlegroups.com
Yeah, I know what you mean. I think someone in this forum put me on to this or an instructor at one of the 10gen courses I attended. I learned it in JavaScript first, then created the Java from that. It was an exercise of pure faith for me too.

My code is about 70%-80% Morphia, which works very well and obviates the whole makePojoFromBson() method I coded (and found nasty, awkward). This is one of the lone things remaining in pure Java BasicDBObject. For now, I'm saying that if it ain't broke, don't fix it.

Best regards,

Russ
Reply all
Reply to author
Forward
0 new messages