Problem while inserting Map<Long, String> into MongoDB

3,010 views
Skip to first unread message

shailesh sukare

unread,
Nov 23, 2010, 6:45:05 AM11/23/10
to mongod...@googlegroups.com, Eliot Horowitz
Hi,

I am using MongoDB Java driver 2.1.

Below is my code to insert data into MongoDB. (I am inserting Map<Long, String>)

        Map<Long, String> mapping = new HashMap<Long, String>();
        mapping.put(1L, "User");
        mapping.put(2L, "Project");
 
        BasicDBObject obj = new BasicDBObject();
        obj.put("info", mapping);

        DBCollection collection = mongoDB.getCollection("mappingtest");
        DBCursor curr = collection.find();
        while(curr.hasNext()) {
              Map<Long, String> out = (Map<Long, String>)curr.next().get("info");
              Long id = out.keySet().iterator().next();   //This should not throw the exception as we put key as Long.
        }

Now the last line is throwing below exception,

Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long

Why? Because when we persist the map in MongoDB, it is converting Map<Long, String> to BasicDBObject and converting all keys in the map from Long to String.
Same is the case with any map with keys other than String, like Map<ObjectId, String>, Map<Integer, String>...

Is there any reason the this conversion? Also, how to store and retrieve map with keys other than String?

--
--
Thanks
-Shailesh

Nat

unread,
Nov 23, 2010, 7:21:21 AM11/23/10
to mongodb-user
Bson standard requires that the key has to be string.

On Nov 23, 7:45 pm, shailesh sukare <shailesh.suk...@gmail.com> wrote:
> Hi,
>
> I am using MongoDB Java driver 2.1.
>
> Below is my code to insert data into MongoDB. (I am inserting Map<Long,
> String>)
>
>         Map<Long, String> mapping = new HashMap<Long, String>();
>         mapping.put(1L, "User");
>         mapping.put(2L, "Project");
>
>         BasicDBObject obj = new BasicDBObject();
>         obj.put("info", mapping);
>
>         DBCollection collection = mongoDB.getCollection("mappingtest");
>         DBCursor curr = collection.find();
>         while(curr.hasNext()) {
>               Map<Long, String> out = (Map<Long,
> String>)curr.next().get("info");
>              * Long id = out.keySet().iterator().next();*   //This should

Brendan W. McAdams

unread,
Nov 23, 2010, 7:31:26 AM11/23/10
to mongod...@googlegroups.com

BSON keys must be strings.

When the Java driver is given a Map it converts it to a BSON object and converts the Map keys to String.

Map's BSON equivalent is a BSON object and as such must have Strings for keys.

On Nov 23, 2010 6:45 AM, "shailesh sukare" <shailes...@gmail.com> wrote:
> Hi,
>
> I am using MongoDB Java driver 2.1.
>
> Below is my code to insert data into MongoDB. (I am inserting Map<Long,
> String>)
>
> Map<Long, String> mapping = new HashMap<Long, String>();
> mapping.put(1L, "User");
> mapping.put(2L, "Project");
>
> BasicDBObject obj = new BasicDBObject();
> obj.put("info", mapping);
>
> DBCollection collection = mongoDB.getCollection("mappingtest");
> DBCursor curr = collection.find();
> while(curr.hasNext()) {
> Map<Long, String> out = (Map<Long,
> String>)curr.next().get("info");
> * Long id = out.keySet().iterator().next();* //This should

> not throw the exception as we put key as Long.
> }
>
> Now the last line is throwing below exception,
>
> Exception in thread "main" java.lang.ClassCastException: java.lang.String
> cannot be cast to java.lang.Long
>
> Why? Because when we persist the map in MongoDB, it is converting Map<Long,
> String> to BasicDBObject and converting all keys in the map from Long to
> String.
> Same is the case with any map with keys other than String, like
> Map<ObjectId, String>, Map<Integer, String>...
>
> Is there any reason the this conversion? Also, how to store and retrieve map
> with keys other than String?
>
> --
> --
> Thanks
> -Shailesh
>
> --
> 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.
>
Reply all
Reply to author
Forward
0 new messages