我需要從 mongodb 獲取資料并使用 Java Spring Reactive Mongodb 庫完成。我為此使用以下代碼:
Criteria criteria = Criteria.where(QUERYFIELD1).in(listOfIds)
.andOperator(Criteria.where(QUERYFIELD2).gte(ChildDate));
Query query = Query.query(criteria).noCursorTimeout();
reactiveMongoTemplate.find(query, Document.class, COLLECTIONNAME);
其中 QUERYFIELD1 是“ChildId”,QUERYFIELD2 是“ChildDate”。以下是我的檔案的結構:
{
"_id": {
"$oid": "6296968fa63757a93e1cd123"
},
"Level1": {
"Level2": [
{
"ChildId": "1234",
"ChildDate": {
"$date": "2021-04-01T04:00:00.000Z"
}
},
{
"ChildId": "5678",
"ChildDate": {
"$date": "2017-05-16T04:00:00.000Z"
}
},
{
"ChildId": "3456",
"ChildDate": {
"$date": "2008-09-16T04:00:00.000Z"
}
},
{
"ChildDate": {
"$date": "2022-06-01T04:00:00.000Z"
},
"ChildId": "7891"
}
]
}
}
我正在嘗試找到一個與 Level2 下的物件中的條件相匹配的檔案。例如,如果我的條件的 ChildId 為“3456”,ChildDate 為“2022-06-01T04:00:00.000Z”,那么我應該得到空結果,因為 ChildId 與 Object3 匹配,而 ChildDate 與 Object4 匹配。但是當我使用以下查詢時,我得到 1 條記錄作為匹配項:
{ "Level1.Level2.ChildId" : "3456", "Level1.Level2.ChildDate" : { $gt: new Date("2022-01-01T05:00:00.000 00:00")}}
我正在嘗試使用 Spring Reactive MongoDB 來實作這一點。請幫忙。
uj5u.com熱心網友回復:
您可以使用它$elemMatch來查找其陣列包含符合條件的專案的檔案:
db.collection.find({
"Level1.Level2": {
$elemMatch: {
ChildId: "3456",
ChildDate: {$gt: new Date("2008-09-16T05:00:00.000Z")}
}
}
})
看看它在操場上的例子是如何作業的
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/495013.html
