MongoDB:只保留第一次约会
我正在寻找一种最简单、合适的解决方案,用mongoDB减少/过滤一个数组,只获取第一个日期(如果为空,则不获取任何内容) 实际上,这是我的疑问:MongoDB:只保留第一次约会,mongodb,aggregate,Mongodb,Aggregate,我正在寻找一种最简单、合适的解决方案,用mongoDB减少/过滤一个数组,只获取第一个日期(如果为空,则不获取任何内容) 实际上,这是我的疑问: $project { firstAction:{ $reduce:{ "input": { $setUnion: ["$fundraisingLog", "$missionLog", "$createdExperiencesLog", "$subscribedExper
$project {
firstAction:{
$reduce:{
"input": { $setUnion: ["$fundraisingLog", "$missionLog", "$createdExperiencesLog", "$subscribedExperiencesLog", "$contributionLog"] },
"initialValue":"01.01.2099",
"in":{
$cond:[
{
$gt:[
{
$dateFromString:{
"dateString":"$$value",
"format":"%d.%m.%Y"
}
},
{
$dateFromString:{
"dateString":"$$this",
"format":"%d.%m.%Y"
}
}
]
},
"$$this",
"$$value"
]
}
}
}
这几乎是完美的,如果数组
$setUnion: ["$fundraisingLog", "$missionLog", "$createdExperiencesLog", "$subscribedExperiencesLog", "$contributionLog"]
是空的,我得到的结果是“01.01.2099”,而不是null或空结果。如果输入数组为空,则默认情况下,
$reduce
将导致初始值
如果您不能像上面的查询或任何其他查询那样进行排序,那么我建议您尝试此方法(此方法应适用于空查询):
示例文档:
/* 1 */
{
"_id" : ObjectId("5d89113d5a0d22d3c8d2aad0"),
"actionLog" : [
"01.01.2014",
"11.01.2014",
"09.01.2014",
"09.09.2014",
"11.01.2015"
]
}
db.yourCollection.aggregate([{ $project: { _id: 0 } }, { $unwind: '$actionLog' , preserveNullAndEmptyArrays: true },
{ $project: { actionLog: { $dateFromString: { dateString: '$actionLog', format: "%d.%m.%Y" } } } }, { $sort: { actionLog: -1 } }, { $limit: 1 }, {
$project: {
actionLog: { $dateToString: { format: "%d.%m.%Y", date: "$actionLog" } }
}
}])
/* 1 */
{
"actionLog" : "11.01.2015"
}
{
"uniqueAction" : [
null,
"18.08.2018",
"04.09.2018",
"05.03.2019",
"07.08.2019",
"12.06.2018",
"13.02.2019",
"13.03.2019",
"15.05.2019",
"15.07.2019",
"16.08.2018",
"17.07.2018",
"18.04.2018",
"18.06.2019",
"18.10.2018",
"21.06.2018",
"21.09.2019",
"22.01.2019",
"22.09.2019",
"23.07.2019",
"23.08.2019",
"24.07.2019",
"25.06.2019",
"25.10.2018",
"26.10.2018",
"27.06.2019",
"31.07.2019"
]
}
{
"_id" : ObjectId("5d849bfaebcbd560107c54a6"),
"info" : {
"firstAction" : "22.09.2019"
}
}
DB查询:
/* 1 */
{
"_id" : ObjectId("5d89113d5a0d22d3c8d2aad0"),
"actionLog" : [
"01.01.2014",
"11.01.2014",
"09.01.2014",
"09.09.2014",
"11.01.2015"
]
}
db.yourCollection.aggregate([{ $project: { _id: 0 } }, { $unwind: '$actionLog' , preserveNullAndEmptyArrays: true },
{ $project: { actionLog: { $dateFromString: { dateString: '$actionLog', format: "%d.%m.%Y" } } } }, { $sort: { actionLog: -1 } }, { $limit: 1 }, {
$project: {
actionLog: { $dateToString: { format: "%d.%m.%Y", date: "$actionLog" } }
}
}])
/* 1 */
{
"actionLog" : "11.01.2015"
}
{
"uniqueAction" : [
null,
"18.08.2018",
"04.09.2018",
"05.03.2019",
"07.08.2019",
"12.06.2018",
"13.02.2019",
"13.03.2019",
"15.05.2019",
"15.07.2019",
"16.08.2018",
"17.07.2018",
"18.04.2018",
"18.06.2019",
"18.10.2018",
"21.06.2018",
"21.09.2019",
"22.01.2019",
"22.09.2019",
"23.07.2019",
"23.08.2019",
"24.07.2019",
"25.06.2019",
"25.10.2018",
"26.10.2018",
"27.06.2019",
"31.07.2019"
]
}
{
"_id" : ObjectId("5d849bfaebcbd560107c54a6"),
"info" : {
"firstAction" : "22.09.2019"
}
}
结果:
/* 1 */
{
"_id" : ObjectId("5d89113d5a0d22d3c8d2aad0"),
"actionLog" : [
"01.01.2014",
"11.01.2014",
"09.01.2014",
"09.09.2014",
"11.01.2015"
]
}
db.yourCollection.aggregate([{ $project: { _id: 0 } }, { $unwind: '$actionLog' , preserveNullAndEmptyArrays: true },
{ $project: { actionLog: { $dateFromString: { dateString: '$actionLog', format: "%d.%m.%Y" } } } }, { $sort: { actionLog: -1 } }, { $limit: 1 }, {
$project: {
actionLog: { $dateToString: { format: "%d.%m.%Y", date: "$actionLog" } }
}
}])
/* 1 */
{
"actionLog" : "11.01.2015"
}
{
"uniqueAction" : [
null,
"18.08.2018",
"04.09.2018",
"05.03.2019",
"07.08.2019",
"12.06.2018",
"13.02.2019",
"13.03.2019",
"15.05.2019",
"15.07.2019",
"16.08.2018",
"17.07.2018",
"18.04.2018",
"18.06.2019",
"18.10.2018",
"21.06.2018",
"21.09.2019",
"22.01.2019",
"22.09.2019",
"23.07.2019",
"23.08.2019",
"24.07.2019",
"25.06.2019",
"25.10.2018",
"26.10.2018",
"27.06.2019",
"31.07.2019"
]
}
{
"_id" : ObjectId("5d849bfaebcbd560107c54a6"),
"info" : {
"firstAction" : "22.09.2019"
}
}
我们可以使用从数组uniqueAction
中查找最大值。以下查询可以获得预期的输出:
db.collection.aggregate([
{
$project:{
"info":{
$reduce:{
"input":"$uniqueAction",
"initialValue":{
"firstAction":"01.01.1970"
},
"in":{
"firstAction":{
$cond:[
{
$gt:[
{
$dateFromString:{
"dateString":"$$this",
"format":"%d.%m.%Y"
}
},
{
$dateFromString:{
"dateString":"$$value.firstAction",
"format":"%d.%m.%Y"
}
}
]
},
"$$this",
"$$value.firstAction"
]
}
}
}
}
}
}
]).pretty()
数据集:
/* 1 */
{
"_id" : ObjectId("5d89113d5a0d22d3c8d2aad0"),
"actionLog" : [
"01.01.2014",
"11.01.2014",
"09.01.2014",
"09.09.2014",
"11.01.2015"
]
}
db.yourCollection.aggregate([{ $project: { _id: 0 } }, { $unwind: '$actionLog' , preserveNullAndEmptyArrays: true },
{ $project: { actionLog: { $dateFromString: { dateString: '$actionLog', format: "%d.%m.%Y" } } } }, { $sort: { actionLog: -1 } }, { $limit: 1 }, {
$project: {
actionLog: { $dateToString: { format: "%d.%m.%Y", date: "$actionLog" } }
}
}])
/* 1 */
{
"actionLog" : "11.01.2015"
}
{
"uniqueAction" : [
null,
"18.08.2018",
"04.09.2018",
"05.03.2019",
"07.08.2019",
"12.06.2018",
"13.02.2019",
"13.03.2019",
"15.05.2019",
"15.07.2019",
"16.08.2018",
"17.07.2018",
"18.04.2018",
"18.06.2019",
"18.10.2018",
"21.06.2018",
"21.09.2019",
"22.01.2019",
"22.09.2019",
"23.07.2019",
"23.08.2019",
"24.07.2019",
"25.06.2019",
"25.10.2018",
"26.10.2018",
"27.06.2019",
"31.07.2019"
]
}
{
"_id" : ObjectId("5d849bfaebcbd560107c54a6"),
"info" : {
"firstAction" : "22.09.2019"
}
}
输出:
/* 1 */
{
"_id" : ObjectId("5d89113d5a0d22d3c8d2aad0"),
"actionLog" : [
"01.01.2014",
"11.01.2014",
"09.01.2014",
"09.09.2014",
"11.01.2015"
]
}
db.yourCollection.aggregate([{ $project: { _id: 0 } }, { $unwind: '$actionLog' , preserveNullAndEmptyArrays: true },
{ $project: { actionLog: { $dateFromString: { dateString: '$actionLog', format: "%d.%m.%Y" } } } }, { $sort: { actionLog: -1 } }, { $limit: 1 }, {
$project: {
actionLog: { $dateToString: { format: "%d.%m.%Y", date: "$actionLog" } }
}
}])
/* 1 */
{
"actionLog" : "11.01.2015"
}
{
"uniqueAction" : [
null,
"18.08.2018",
"04.09.2018",
"05.03.2019",
"07.08.2019",
"12.06.2018",
"13.02.2019",
"13.03.2019",
"15.05.2019",
"15.07.2019",
"16.08.2018",
"17.07.2018",
"18.04.2018",
"18.06.2019",
"18.10.2018",
"21.06.2018",
"21.09.2019",
"22.01.2019",
"22.09.2019",
"23.07.2019",
"23.08.2019",
"24.07.2019",
"25.06.2019",
"25.10.2018",
"26.10.2018",
"27.06.2019",
"31.07.2019"
]
}
{
"_id" : ObjectId("5d849bfaebcbd560107c54a6"),
"info" : {
"firstAction" : "22.09.2019"
}
}
从你们的另一个问题来看-,从这个问题的描述来看-,若我理解正确,当你们说空结果时,在你们上面的数组中它是“null”吗?这就是你的聚合结果,我不认为你能够在一个日期数组上排序,你可以在该数组上进行展开/转换为日期/排序/限制,但不确定排序在这种日期格式上是否能正常工作!!!!或者您可以对日期字段进行排序/限制,然后将其转换为字符串格式。为什么有两个键具有相同的名称?哪些键具有相同的名称?我对您的编辑感到有点困惑:询问“firstAction”和“actionLog”…是的。我想现在更清楚了。谢谢我尝试过,但结果为空:/n我可以在同一个$project中执行吗$项目:{$setUnion:[“$uniqueMissions”、“$uniqueFundraising”、“$UniqueCreateExperiences”、“$uniqueSubscribedExperiences”、“$uniqueContributions”],},firstConnexion:{$reduce:{…}“errmsg”:“已找到不完整的日期/时间字符串,缺少元素:\“01.2019”,@Kaherdin请分享您的mongo查询