使用 mongodb 聚合管道按升序将日期添加到集合中的所有记录
Add date to all records in a collection in ascending order using mongodb aggregation pipeline
是否可以在聚合中为特定日期的所有记录添加日期
[
{
_id: "bmasndvhjbcw",
name: "lucas",
occupation: "scientist",
present_working:true,
age: 55,
location: "texas",
},
{
_id: "bmasndvhjbcx",
name: "mark",
occupation: "scientist",
age: 45,
present_working:true,
location: "texas",
},
{
_id: "bmasndvhjbcq",
name: "cooper",
occupation: "physicist",
age: 69,
location: "texas",
}
]
有没有一种方法可以像这样
从这个日期 date:2019-11-25T01:00:00.000+00:00
开始按升序添加所有记录的日期
[
{
_id: "bmasndvhjbcw",
name: "lucas",
occupation: "scientist",
present_working:true,
age: 55,
location: "texas",
date:2019-11-25T01:00:00.000+00:00
},
{
_id: "bmasndvhjbcx",
name: "mark",
occupation: "scientist",
age: 45,
present_working:true,
location: "texas",
date:2019-11-26T01:00:00.000+00:00
},
{
_id: "bmasndvhjbcq",
name: "cooper",
occupation: "physicist",
age: 69,
location: "texas",
date:2019-11-27T01:00:00.000+00:00
}
]
mongodb 版本 4.0
这个聚合得到了预期的结果:
THIS_DATE = ISODate("2019-11-25T01:00:00.000+00:00")
db.collection.aggregate( [
{
$group: {
_id: null,
docs: { $push: "$$ROOT" }
}
},
{
$project: {
_id: 0,
docs: {
$map: {
input: { $range: [ 0, { $size: "$docs" } ] },
in: {
$mergeObjects: [
{ date: { $add: [ THIS_DATE, { $multiply: [ "$$this", 24*60*60*1000 ] } ] } },
{ $arrayElemAt: [ "$docs", "$$this" ] }
]
}
}
}
}
},
{
$unwind: "$docs"
},
{
$replaceRoot: { newRoot: "$docs" }
}
] )
您可以通过以下查询实现此目的:
db.collection.aggregate([
{
$group: {
_id: null,
docs: {
$push: "$$ROOT"
}
}
},
{
$unwind: {
path: "$docs",
includeArrayIndex: "index"
}
},
{
$addFields: {
"docs.date": {
$add: [
{
$dateFromString: {
dateString: "2019-11-25T01:00:00.000"
}
},
{
$multiply: [
"$index",
{
$multiply: [
24,
60,
60,
1000
]
}
]
}
]
}
}
},
{
$replaceRoot: {
newRoot: "$docs"
}
}
])
测试一下here
是否可以在聚合中为特定日期的所有记录添加日期
[
{
_id: "bmasndvhjbcw",
name: "lucas",
occupation: "scientist",
present_working:true,
age: 55,
location: "texas",
},
{
_id: "bmasndvhjbcx",
name: "mark",
occupation: "scientist",
age: 45,
present_working:true,
location: "texas",
},
{
_id: "bmasndvhjbcq",
name: "cooper",
occupation: "physicist",
age: 69,
location: "texas",
}
]
有没有一种方法可以像这样
从这个日期date:2019-11-25T01:00:00.000+00:00
开始按升序添加所有记录的日期
[
{
_id: "bmasndvhjbcw",
name: "lucas",
occupation: "scientist",
present_working:true,
age: 55,
location: "texas",
date:2019-11-25T01:00:00.000+00:00
},
{
_id: "bmasndvhjbcx",
name: "mark",
occupation: "scientist",
age: 45,
present_working:true,
location: "texas",
date:2019-11-26T01:00:00.000+00:00
},
{
_id: "bmasndvhjbcq",
name: "cooper",
occupation: "physicist",
age: 69,
location: "texas",
date:2019-11-27T01:00:00.000+00:00
}
]
mongodb 版本 4.0
这个聚合得到了预期的结果:
THIS_DATE = ISODate("2019-11-25T01:00:00.000+00:00")
db.collection.aggregate( [
{
$group: {
_id: null,
docs: { $push: "$$ROOT" }
}
},
{
$project: {
_id: 0,
docs: {
$map: {
input: { $range: [ 0, { $size: "$docs" } ] },
in: {
$mergeObjects: [
{ date: { $add: [ THIS_DATE, { $multiply: [ "$$this", 24*60*60*1000 ] } ] } },
{ $arrayElemAt: [ "$docs", "$$this" ] }
]
}
}
}
}
},
{
$unwind: "$docs"
},
{
$replaceRoot: { newRoot: "$docs" }
}
] )
您可以通过以下查询实现此目的:
db.collection.aggregate([
{
$group: {
_id: null,
docs: {
$push: "$$ROOT"
}
}
},
{
$unwind: {
path: "$docs",
includeArrayIndex: "index"
}
},
{
$addFields: {
"docs.date": {
$add: [
{
$dateFromString: {
dateString: "2019-11-25T01:00:00.000"
}
},
{
$multiply: [
"$index",
{
$multiply: [
24,
60,
60,
1000
]
}
]
}
]
}
}
},
{
$replaceRoot: {
newRoot: "$docs"
}
}
])
测试一下here