获取 $project 中两个日期之间的差异
get difference between two dates in $project
试图预测两个日期之间的日期差异,但出现错误 -
Invalid $project :: caused by :: Unknown expression $dateDiff
db.books.aggregate([{
$project:{
Date_diff:
{$dateDiff:{
start_dt:'$borrowers_list.borrowed_dt',
endDate:'$borrowers_list.return_dt',
unit: "day"
}
}
}
}])
json文档结构是这样的-
_id:6188a5283543f7cc2f77c73f
branch_id:1
borrowers_list:Object
0:Object
borrowed_dt:2021-08-15T06:00:00.000+00:00
card_no:"ID000067"
return_dt:2021-08-25T06:00:00.000+00:00
我不知道为什么错误是未知表达式 $dateDiff,因为我的语法是正确的。有人有什么建议吗?
根据您提供的JSON文档,文档应该如下(如有错误请指正):
{
_id: ObjectId("6188a5283543f7cc2f77c73f"),
branch_id: 1,
borrowers_list: {
0: {
borrowed_dt: ISODate("2021-08-15T06:00:00.000+00:00"),
card_no: "ID000067",
return_dt: ISODate("2021-08-25T06:00:00.000+00:00")
}
}
}
]
$dateFiff
字段中没有start_dt
,是startDate
。
Query
db.collection.aggregate([
{
$project: {
Date_diff: {
$dateDiff: {
startDate: "$borrowers_list.0.borrowed_dt",
endDate: "$borrowers_list.0.return_dt",
unit: "day"
}
}
}
}
])
注意:上面的查询将对borrowers_list
中的第一个文档执行$dateDiff
。
如果您需要迭代 borrowers_list
中的每个文档(具有键值对)以执行 $dateDiff
。
$set
- 从对象到数组(通过 $objectToArray
)将 borrowers_list
转换为新字段 borrowers
.
$set
- 迭代 borrowers
数组 (1) 中的每个文档并执行 $dateDiff
.
$project
- 修饰输出文档,将 Date_diff
从数组转换为对象(通过 $objectToArray
)。
Query
db.collection.aggregate([
{
$set: {
borrowers: {
"$objectToArray": "$borrowers_list"
}
}
},
{
$set: {
Date_diff: {
$map: {
input: "$borrowers",
as: "b",
in: {
k: "$$b.k",
v: {
$dateDiff: {
startDate: "$$b.v.borrowed_dt",
endDate: "$$b.v.return_dt",
unit: "day"
}
}
}
}
}
}
},
{
$project: {
Date_diff: {
"$arrayToObject": "$Date_diff"
}
}
}
])
Sample Mongo Playground (Iterate document with key-value pair)
试图预测两个日期之间的日期差异,但出现错误 -
Invalid $project :: caused by :: Unknown expression $dateDiff
db.books.aggregate([{
$project:{
Date_diff:
{$dateDiff:{
start_dt:'$borrowers_list.borrowed_dt',
endDate:'$borrowers_list.return_dt',
unit: "day"
}
}
}
}])
json文档结构是这样的-
_id:6188a5283543f7cc2f77c73f
branch_id:1
borrowers_list:Object
0:Object
borrowed_dt:2021-08-15T06:00:00.000+00:00
card_no:"ID000067"
return_dt:2021-08-25T06:00:00.000+00:00
我不知道为什么错误是未知表达式 $dateDiff,因为我的语法是正确的。有人有什么建议吗?
根据您提供的JSON文档,文档应该如下(如有错误请指正):
{
_id: ObjectId("6188a5283543f7cc2f77c73f"),
branch_id: 1,
borrowers_list: {
0: {
borrowed_dt: ISODate("2021-08-15T06:00:00.000+00:00"),
card_no: "ID000067",
return_dt: ISODate("2021-08-25T06:00:00.000+00:00")
}
}
}
]
$dateFiff
字段中没有start_dt
,是startDate
。
Query
db.collection.aggregate([
{
$project: {
Date_diff: {
$dateDiff: {
startDate: "$borrowers_list.0.borrowed_dt",
endDate: "$borrowers_list.0.return_dt",
unit: "day"
}
}
}
}
])
注意:上面的查询将对borrowers_list
中的第一个文档执行$dateDiff
。
如果您需要迭代 borrowers_list
中的每个文档(具有键值对)以执行 $dateDiff
。
$set
- 从对象到数组(通过$objectToArray
)将borrowers_list
转换为新字段borrowers
.$set
- 迭代borrowers
数组 (1) 中的每个文档并执行$dateDiff
.$project
- 修饰输出文档,将Date_diff
从数组转换为对象(通过$objectToArray
)。
Query
db.collection.aggregate([
{
$set: {
borrowers: {
"$objectToArray": "$borrowers_list"
}
}
},
{
$set: {
Date_diff: {
$map: {
input: "$borrowers",
as: "b",
in: {
k: "$$b.k",
v: {
$dateDiff: {
startDate: "$$b.v.borrowed_dt",
endDate: "$$b.v.return_dt",
unit: "day"
}
}
}
}
}
}
},
{
$project: {
Date_diff: {
"$arrayToObject": "$Date_diff"
}
}
}
])
Sample Mongo Playground (Iterate document with key-value pair)