gpt4 book ai didi

MongoDB 如何添加条件 $match 条件

转载 作者:可可西里 更新时间:2023-11-01 09:58:32 25 4
gpt4 key购买 nike

我有以下架构

{
f1: "test",
f2: "something",
type: "A",
date: "2018-11-01T00:00:00.000Z",
deleted: false
},
{
"f1": "check",
type: "B",
deleted: false
}

现在我想要的是获取所有数据,如果 type = "A",则向我的匹配查询添加一个额外的条件,假设将其日期与当前日期进行比较。我当前的查询是:

db.getCollection('myCollection').aggregate([
{$match:{
{"deleted":false},
// I want to check if type is A then compare its date
}}
])

最佳答案

您可以尝试输入 $or 并说“如果不是类型 A or,则日期为 x”:

{$match:{
$and: [
{deleted: false},
$or: [
{type: {$ne: 'A'}},
{date: {$gte: ISODate("2018-01-01T00:00:00.0Z"), $lt: ISODate("2018-06-01T00:00:00.0Z")}}
]
]
}}

关于MongoDB 如何添加条件 $match 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53081756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com