0

I have a Json document in MongoDB collection that I would like to query based on _id.

{
  "_id": {
    "$oid": "102e8484f2caba7e805dc334"
  },
  "customerName": "TESLA",
  "customerInfo": {
    "name": "Elon",
  },
  "createdDate": {
    "$date": "2021-02-19"
  },
  "modifiedDate": {
    "$date": "2021-02-18T15:19:00.586Z"
  },
  "status": "DRAFT",  
}

Now I have a working code with which if I query any other json key which is not system generated (say status or customerInfo.name), I am able to get the expected results.

But all trials that I did with _id or $date, did not work. I tried looking for answers in multiple posts but none of them worked. These are search options I tried before asking this question here:

import static com.mongodb.client.model.Filters.eq;

// above import used below.
MongoCursor<Document> cursor = collection.find(eq(key, value)).iterator();
where 
key="_id.$oid"
value= byID (passed as say "123123121"

eq("_id.$oid", byID)
eq("_id.oid", byID)
eq("_id", byID)
eq("oid", byID)
eq("oid", ObjectId(byID)) - this gives compile errors. 

Any help is much appreciated!

Pramod Yadav
  • 467
  • 8
  • 14

1 Answers1

0

Found this answer on Slack (which I found to be working - tested Okay): How to query documents using "_id" field in Java mongodb driver?

by @bogolyandras with heading "For those who are seeking a more up to date method, especially with 3.4:"

Pramod Yadav
  • 467
  • 8
  • 14