如何在mongodb的$ cond else部分中使用$ match

I want to use $match in $cond in Mongo query and my first try is :

Query in MongodbPlayground

但是发生了此错误:

查询失败:(InvalidPipelineOperator)无法识别的表达式'$ match'

Do you have any idea of using $match in $cond?

我的预期结果是:

[
  {
    "_id": "b4",
    "coll_C": "c2",        // the nearest date to "2020-08-01" that has coll_C value is: "2020-07-01" so added the "coll_C:"c2"
    "coll_D": "d1",
    "coll_E": "e2",
    "date": "2020-08-01"
  },
  {
    "_id": "b2",
    "coll_C": "c2",
    "coll_D": "d2",        // the nearest date to "2020-07-01" that has coll_D value is: "2020-06-01" so added the "coll_D:"d2"
    "coll_E": "e1",        // the nearest date to "2020-07-01" that has coll_E value is: "2020-05-01" so added the "coll_E:"e1"
    "date": "2020-07-01"
  },
  {
    "_id": "b1",
    "coll_C": "c1",
    "coll_D": "d2",
    "coll_E": "e1",        // the nearest date to "2020-06-01" that has coll_E value is: "2020-05-01" so added the "coll_E:"e1"
    "date": "2020-06-01"
  },
  {
    "_id": "b3",
    "coll_C": "c3",
    "coll_D": "d3",
    "coll_E": "e1",
    "date": "2020-05-01"
  }
]
  • if it has each of the coll_C or coll_D or coll_E, show that.
  • if it has not each of the coll_C or coll_D or coll_E, get the nearest coll_C or coll_D or coll_E base on date field from other documents.
评论