MongoDB: $in with an ObjectId array

lcappa picture lcappa · Sep 10, 2014 · Viewed 23.9k times · Source

Just a quick question about something I've just experienced and I'm still thinking about why:

mongos> db.tickets.count({ "idReferenceList" : { "$in" : [ { "$oid" : "53f1f09f2cdcc8f339e5efa2"} , { "$oid" : "5409ae2e2cdc31c5aa0ce0a5"}]}});

0

mongos> db.tickets.count({ "idReferenceList" : { "$in" : [ ObjectId("53f1f09f2cdcc8f339e5efa2") , ObjectId("5409ae2e2cdc31c5aa0ce0a5")]}});

2

I thought that both $oid and ObjectId spelling formats where exactly the same for MongoDB. Does anyone know why with the first query return 0 results and with the second one is returning 2 (the right answer)?

Furthermore, I'm using Morphia framework which uses MongoDB Java driver to interact with MongoDB. I've realised that there exists a problem by searching with $in operator in ObjectIds arrays over fields that are not _id by executing this lines of code:

List< ObjectId > fParams = new ArrayList< ObjectId >();

fParams.add(...);

Query<Ticket> query = genericDAO.createQuery();

query.field("idReferenceList").in(fParams);

result = genericDAO.find(query).asList();

Thank you very much in advance.

Regards,

  • Luis Cappa

Answer

BatScream picture BatScream · Sep 10, 2014

Both these formats are valid representations of an object id in MongoDB, according to the documentation,

http://docs.mongodb.org/manual/reference/mongodb-extended-json/

and they represented differently in the two modes,

    Strict Mode         mongo Shell Mode
    -----------         ----------------

   { "$oid": "<id>" }  ObjectId( "<id>" )

So, to query fields which contain objectid, from the shell/console mode, you need to use ObjectId("<id>"). Which is the syntax to be followed in the mongo shell mode.

Hence the query:

db.tickets.count({ "idReferenceList" : { "$in" : [ ObjectId("53f1f09f2cdcc8f339e5efa2") , ObjectId("5409ae2e2cdc31c5aa0ce0a5")]}});

would return you row count.

Now to do it via the Java API,

You need to do it as below:

String[] ids = {"53f1f09f2cdcc8f339e5efa2","5409ae2e2cdc31c5aa0ce0a5"};
ObjectId[] objarray = new ObjectId[ids.length];

for(int i=0;i<ids.length;i++)
{
    objarray[i] = new ObjectId(ids[i]);
}

BasicDBObject inQuery = new BasicDBObject("$in", objarray);
BasicDBObject query = new BasicDBObject("idReferenceList", inQuery);
DBCursor cursor = db.collection.find(query);
while(cursor.hasNext())
{
    DBObject doc = cursor.next();
    // process the doc.
}