我在 MongoDB 中有一個問題。我需要找到陣列只包含假鍵的所有專案
我的物品看起來像這樣
{
"field1" : "firstTest",
"field2" : [
{
"name" : "test1",
"use" : false
},
{
"name" : "test2",
"use" : true
},
{
"name" : "test3",
"use" : false
}
],
"updatedAt" : 2019-02-03T23:00:00.000 00:00
}
我需要將 field2 陣列僅包含“use”鍵為 false 且“updatedAt”日期小于 3 年的所有專案匹配
我從以下內容開始:
await client.db(db).collection(collection).aggregate([{
$match:{
$and:[
{
field2: { $elemMatch: { use: { $all: [false] } }}
},
{
updatedAt: { $lt: moment().subtract(3,'years').toDate()}
}
]
}
}])
但看起來我的 field2 查詢不起作用
你知道如何匹配陣列中的布爾屬性嗎?
提前致謝
uj5u.com熱心網友回復:
使用$not和$elemMatch
在mongoPlayground進行測驗
await client.db(db).collection(collection).aggregate([
{
"$match": {
"$and": [
{
"field2": {
$not: {
$elemMatch: {
use: {
$ne: false
}
}
}
}
},
{
updatedAt: { $lt: moment().subtract(3,'years').toDate()}
}
]
}
}
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/444036.html
標籤:mongodb
下一篇:從檔案中查詢嵌套陣列
