tags:

views:

26

answers:

1

Hello, This is my current query: Using Java+mongoDB

    {
    BasicDBObject select = new BasicDBObject();
    select.put("info.name.fn", 1);

    DBCursor cursor = collection.find(new BasicDBObject(), select);
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
    }

It gives an output as:

{ "_id" : { "$oid" : "123"} , "info" : { "name" : { "fn" : "foo"}}}
{ "_id" : { "$oid" : "123"} , "info" : { "name" : { "fn" : "bar"}}}
{ "_id" : { "$oid" : "123"} , "info" : { "name" : { "fn" : "baz"}}}

_ids changed to accommodate the output. My question is, what query do I give to get the output as:

foo
bar
baz

Is it even possible? Or does every query always return it in the above format? I cant run a distinct() because there are duplicate names.

Thanks.

+1  A: 

The minimal query result you can get is the one you show above.

You do not have to print all of that, though.

 System.out.println(cursor.next().get("info").get("name").get("fn"));
Thilo
yes, that worked. except that i had to add a BasicBSONObject cast twice. now it looks like this: System.out.println(((BasicBSONObject) ((BasicBSONObject) cursor.next().get("info")).get("name")).get("fn"))
Kaustubh P
Oh. That casting is ugly. But I suppose there is nothing you can do about that.
Thilo
yeah. i will keep the question unanswered just a bit longer to see if i can hook some other answers. posted it on mongodb-user too.
Kaustubh P