mongodb - MongoDB如何添加条件$match条件
问题描述
我有以下架构
{
f1: "test",
f2: "something",
type: "A",
date: "2018-11-01T00:00:00.000Z",
deleted: false
},
{
"f1": "check",
type: "B",
deleted: false
}
现在我想要的是获取所有数据,如果type = "A", then add an extra condition to my match query, suppose compare its date with current date
. 我目前的查询是:
db.getCollection('myCollection').aggregate([
{$match:{
{"deleted":false},
// I want to check if type is A then compare its date
}}
])
解决方案
您可以尝试$or
并说“如果它不是类型 A或日期是 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")}}
]
]
}}
推荐阅读
- sql-server - 如何在 sql server2012 中将 datetime 转换为 int?
- python - 如何使用 python (ucrtbase.DLL) 解析 ucrtbase.dll APPCRASH
- java - 如何在 java 中使用 ews 从电子邮件中获取“收件人”信息
- ruby - Fluent D record_transformer 插件和 Ruby,最终与 elasticsearch、Kibana 一起使用
- python - 向 Google Analtycis API 发出请求会返回 Login Required
- jmeter - 在 Windows 中执行 jmeter.sh
- r - 如何更改 Shiny Dashboard Body 的背景颜色
- iphone - Web RTC , 视频通话 我们可以在 ios 上录制远程 RTCMediaStream
- build - VSTS 构建定义在获取源步骤失败
- unity3d - 仅在 touchphase 结束时触发碰撞器