我有一個 MongoDB 資料庫,其中包含以下格式的檔案:
{
"name": "",
"surname": "",
"ssn": "",
"tests": [
{
"date": "",
"attending": [
{
"id": "",
"role": ""
}
]
}
]
}
一份檔案包含每個人的測驗串列及其相關資訊。我想從每個檔案中僅洗掉在某個日期 (2019-02-22) 之后由某個護士 (id = 0187) 執行的子檔案(屬于測驗陣列)。
由于所有日期都以字串格式保存,為了進行日期比較,我$dateFromString按以下方式使用了運算子:
db.Vaccinations.aggregate([
"$match": {
"$expr": {
"$and": [
{
"$gte": [
{ "$dateFromString": { "dateString": "$tests.date"}},
ISODate("2019-02-22T00:00:00Z")
]},
{
"tests.attending.id" : "0187"
}
]
}
}
])
但是我無法使用$pull運算子洗掉相應的子檔案。是否有可能這樣做或者是$pull錯誤的操作員?
uj5u.com熱心網友回復:
詢問
- 從測驗中只保留那些
- 或者
date < "2019-02-22"nurse != "0187"
通過這種方式的測驗必須在日期之前或由另一名護士完成。
nurse != "0187"使用路徑“$test.attending.id”檢查以獲取該測驗的所有 ID(一個陣列),并測驗其交集["0187"]是否為空 => 護士沒有進行測驗。
*您可以根據需要使用 updateOne 或 updateMany
*$dateFromString如果日期具有相同的格式,我沒有使用,例如
YYYY-MM-DD比較可以作業,但通常將日期保存在資料庫中,它使事情變得更容易和更快。
測驗代碼在這里
update(
{},
[{"$set":
{"tests":
{"$filter":
{"input": "$tests",
"cond":
{"$or":
[{"$lt": ["$$test.date", "2019-02-22"]},
{"$eq":
[{"$setIntersection": [["0187"], "$$test.attending.id"]}, []]}]},
"as": "test"}}}}])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/372918.html
標籤:MongoDB mongodb-查询 聚合框架 子文件
