mongodbmongo-jackson-mapper

find set of records in mongodb/jackson driver user a List of ids?


once again I'm spending a lot of frustrating hours trying to figure out something which sounds rudimentary with mongodb and jackson client but being unsuccessful.sigh! So I have a list of User ids and I'm trying to find the users which match those ids, but it seems i cannot get it to work, I have tried all the following queries and none returns any data. any help is greately appreciated:

public static List<User> getUsersInfo(List<String> ids) {
    Logger.debug("ids" + ids);

    DBCursor<User> cursor = coll.find().in("_id",ids);
    System.out.println(cursor.size());

    cursor = coll.find().in("id",ids);
    System.out.println(cursor.size());

    cursor = coll.find(DBQuery.in("id", ids));
    System.out.println(cursor.size());

    cursor = coll.find(DBQuery.in("_id", ids));
    System.out.println(cursor.size());  

output:

[debug] application - ids[51eb40b73004b5cf0960505a, 51eb41de3004b2496a916177,   51eb42023004b2496a916178]
0
0
0
0

mongodb data:

db.users.find()
{ "_id" : ObjectId("51eb40763004b5cf09605055"), "email" : " ", ...}
{ "_id" : ObjectId("51eb40b73004b5cf0960505a"), "email" : " ",...}
{ "_id" : ObjectId("51eb41de3004b2496a916177"), "email" :"",...}
{ "_id" : ObjectId("51eb42023004b2496a916178"), "email" : "", ... }

Solution

  • So i figured this out, the documents are completely lacking and misleading, so in the contrast to the findById method by JacksonCollection which can take a String as an Id, you cannot use id with the type string in other places, the id is defined as ObjectId so the array that you are sending must be type ObjectId

    public static List<User> getUsersInfo(List<org.bson.types.ObjectId> ids) {
    .
    .
    .
    }