通过比较Mongo DB中的田间日期来获取行
我试图比较两个集合本身以获取行,但无法获得行。
[{
"date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
"date_modified" : ISODate("2022-06-29T05:01:15.370+0000"),
},
{
"date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
"date_modified" : ISODate("2022-06-19T05:01:15.370+0000"),
},
{
"date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
}]
查询
db.getCollection("collection_name").aggregate([
{
$match: {
status: '1',
$or: [
{
date_modified: { $gt: ISODate('$date_created') }
},
{
date_modified: {
"$exists": false,
},
},
],
},
},
]);
预期结果:
[{
"date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
"date_modified" : ISODate("2022-06-29T05:01:15.370+0000"),
},{
"date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
}]
当前结果: date_created尚未定义
I am trying to compare two fields of collection mongo itself to get rows, but not able to get it.
[{
"date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
"date_modified" : ISODate("2022-06-29T05:01:15.370+0000"),
},
{
"date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
"date_modified" : ISODate("2022-06-19T05:01:15.370+0000"),
},
{
"date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
}]
Query
db.getCollection("collection_name").aggregate([
{
$match: {
status: '1',
$or: [
{
date_modified: { $gt: ISODate('$date_created') }
},
{
date_modified: {
"$exists": false,
},
},
],
},
},
]);
Expected result:
[{
"date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
"date_modified" : ISODate("2022-06-29T05:01:15.370+0000"),
},{
"date_created" : ISODate("2022-06-24T05:01:15.370+0000"),
}]
Current result: date_created is not defined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
$ expr
进行字段之间的比较。使用$ ifnull
来满足您检查date_created
字段的存在的要求。这是 mongo playground 供您参考。
You need to use
$expr
for comparison between fields. Use$ifNull
to cater your requirement for checking the existence for thedate_created
field.Here is the Mongo Playground for your reference.