【发布时间】:2021-02-19 17:57:10
【问题描述】:
我在 MongoDB 集合中有一个 Json 文档,我想根据 _id 进行查询。
{
"_id": {
"$oid": "102e8484f2caba7e805dc334"
},
"customerName": "TESLA",
"customerInfo": {
"name": "Elon",
},
"createdDate": {
"$date": "2021-02-19"
},
"modifiedDate": {
"$date": "2021-02-18T15:19:00.586Z"
},
"status": "DRAFT",
}
现在我有了一个工作代码,如果我查询任何其他非系统生成的 json 键(比如 status 或 customerInfo.name),我就能得到预期的结果。
但是我用 _id 或 $date 进行的所有试验都没有奏效。我尝试在多个帖子中寻找答案,但没有一个有效。这些是我在这里问这个问题之前尝试过的搜索选项:
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.
非常感谢任何帮助!
【问题讨论】:
标签: java mongodb mongodb-query