19

I'm trying to get mongodb query working. Collection comes in the format:

{
"_id": {
    "$oid": "54651022bffebc03098b4567"
},
"browser": "ie",
"browser_version": "10.0 Desktop",
"os_version": "8",
"device": null,
"os": "Windows"
}

The following works:

{
    "_id": {
        "$in": [
            {
                "$oid": "54651022bffebc03098b4567"
            },
            {
                "$oid": "54651022bffebc03098b4568"
            }
        ]
   }
}

However, I get a syntax error for the following:

{
    "_id": {
        "$in": [
            ObjectId("54651022bffebc03098b4567"),
            ObjectId("54651022bffebc03098b4568")
        ]
    }
}

There are a similar questions that suggested that ObjectId should work:

How to create query with ObjectIds using java?

$all parameter in mongodb does not work with ObjectId list

Community
  • 1
  • 1
Vlad Vinnikov
  • 1,457
  • 3
  • 22
  • 33
  • 2
    Which client are you using to enter and run these queries? The mongo shell? A Java program? (You link to a Java article at the bottom.) While many of the concepts and details are similar across all clients, many have their own particular quirks that make them unique. For example, the ObjectId() syntax in your third code block I would expect to work only in JavaScript (including the mongo shell). – jared Nov 14 '14 at 21:36
  • Wait your trying this in Java? That's not how you make objects in Java – Sammaye Nov 14 '14 at 21:57
  • I'm running queries in Mongolab browser. I also tried running the same query in php with MongoId() constructor that worked fine – Vlad Vinnikov Nov 14 '14 at 22:00

1 Answers1

29

The MongoLab UI uses Strict MongoDB Extended JSON so Object IDs are represented thusly, as in the second code block of the OP:

{ "$oid": "<id>" }
jared
  • 5,369
  • 2
  • 21
  • 24