使用 Mongoose 在 .find() 上排序 DD/MM 日期
Sort DD/MM date on a .find() with Mongoose
我试图找到如何使用猫鼬执行的 .find() 按天和月对生日日期进行排序。
实际代码:
const embed2 = new MessageEmbed()
.setColor('#FBC78E')
.setTitle(`Liste des anniversaires de ${message.guild.name}`)
birthday.find({}).then(data => {
embed2
.setDescription(Array.from(data).map(u => `<@${u.user_id}> : ${u.birthday}`))
loading_message.delete();
message.guild.channels.resolve(args[1]).send(embed2)
});
问题是,当我尝试将 .sort('birthday') 添加到我的 .find() 时,它只会过滤日期,但这不是我要搜索的内容,这是.find() 的结果(没有 .sort()):
[
{
_id: new ObjectId("620d763c86159915746f431b"),
user_id: '493470054415859713',
birthday: '04/11',
__v: 0
},
{
_id: new ObjectId("620d770505fa9fe0910968c0"),
user_id: '933751655046979645',
birthday: '18/05',
__v: 0
},
{
_id: new ObjectId("620d77575417ab94a5f2c837"),
user_id: '735987183714041867',
birthday: '15/12',
__v: 0
},
{
_id: new ObjectId("620d85fed17e202ed6d2b847"),
user_id: '320851795058360331',
birthday: '03/11',
__v: 0
}
]
异常结果:
[
{
_id: new ObjectId("620d770505fa9fe0910968c0"),
user_id: '933751655046979645',
birthday: '18/05',
__v: 0
},
{
_id: new ObjectId("620d85fed17e202ed6d2b847"),
user_id: '320851795058360331',
birthday: '03/11',
__v: 0
},
{
_id: new ObjectId("620d763c86159915746f431b"),
user_id: '493470054415859713',
birthday: '04/11',
__v: 0
},
{
_id: new ObjectId("620d77575417ab94a5f2c837"),
user_id: '735987183714041867',
birthday: '15/12',
__v: 0
}
]
正如您在这里看到的,生日是按日期和月份排序的,这是我尝试做的。
谢谢
当您对字段值“04/11”、“18/05”、“15/12”、“03/11”进行排序时 - 排序为“03/11”、“04/11” ,“15/12”,“18/05”。它由字符串值组成——其中“0”小于“1”,依此类推。由于 birthday
数据的格式为“DD/MM”,因此您无法获得所需的正确排序,即按 MM 和 DD。数据格式需要为“MM/DD”。这样做的方法是转换格式,然后用新格式化的值排序。
例如,您可以使用聚合运算符将birthday
字段中的DD和MM的值交换,并创建一个名为birthday_MM_DD
的新字段并用于排序。请注意,代码在 mongosh
或 mongo
shell.
中运行
var pipeline = [
// Create a new birthday field with MM/DD format (a temporary field)
{
$addFields: {
birthday_MM_DD: {
$concat: [
{ $substrCP: [ "$birthday", 3, 2 ] },
"/",
{ $substrCP: [ "$birthday", 0, 2 ] }
]
}
}
},
// Sort with the newly formatted birthday field
{
$sort: {
birthday_MM_DD: 1
}
},
// Remove the temporary birthday field
// (as it is no longer needed)
{
$unset:
"birthday_MM_DD"
},
]
db.collection.aggregate(pipeline)
结果按 birthday
正确排序 格式 的文档,正如预期的那样:
"18/05", "03/11", "04/11", "15/12"
我试图找到如何使用猫鼬执行的 .find() 按天和月对生日日期进行排序。
实际代码:
const embed2 = new MessageEmbed()
.setColor('#FBC78E')
.setTitle(`Liste des anniversaires de ${message.guild.name}`)
birthday.find({}).then(data => {
embed2
.setDescription(Array.from(data).map(u => `<@${u.user_id}> : ${u.birthday}`))
loading_message.delete();
message.guild.channels.resolve(args[1]).send(embed2)
});
问题是,当我尝试将 .sort('birthday') 添加到我的 .find() 时,它只会过滤日期,但这不是我要搜索的内容,这是.find() 的结果(没有 .sort()):
[
{
_id: new ObjectId("620d763c86159915746f431b"),
user_id: '493470054415859713',
birthday: '04/11',
__v: 0
},
{
_id: new ObjectId("620d770505fa9fe0910968c0"),
user_id: '933751655046979645',
birthday: '18/05',
__v: 0
},
{
_id: new ObjectId("620d77575417ab94a5f2c837"),
user_id: '735987183714041867',
birthday: '15/12',
__v: 0
},
{
_id: new ObjectId("620d85fed17e202ed6d2b847"),
user_id: '320851795058360331',
birthday: '03/11',
__v: 0
}
]
异常结果:
[
{
_id: new ObjectId("620d770505fa9fe0910968c0"),
user_id: '933751655046979645',
birthday: '18/05',
__v: 0
},
{
_id: new ObjectId("620d85fed17e202ed6d2b847"),
user_id: '320851795058360331',
birthday: '03/11',
__v: 0
},
{
_id: new ObjectId("620d763c86159915746f431b"),
user_id: '493470054415859713',
birthday: '04/11',
__v: 0
},
{
_id: new ObjectId("620d77575417ab94a5f2c837"),
user_id: '735987183714041867',
birthday: '15/12',
__v: 0
}
]
正如您在这里看到的,生日是按日期和月份排序的,这是我尝试做的。 谢谢
当您对字段值“04/11”、“18/05”、“15/12”、“03/11”进行排序时 - 排序为“03/11”、“04/11” ,“15/12”,“18/05”。它由字符串值组成——其中“0”小于“1”,依此类推。由于 birthday
数据的格式为“DD/MM”,因此您无法获得所需的正确排序,即按 MM 和 DD。数据格式需要为“MM/DD”。这样做的方法是转换格式,然后用新格式化的值排序。
例如,您可以使用聚合运算符将birthday
字段中的DD和MM的值交换,并创建一个名为birthday_MM_DD
的新字段并用于排序。请注意,代码在 mongosh
或 mongo
shell.
var pipeline = [
// Create a new birthday field with MM/DD format (a temporary field)
{
$addFields: {
birthday_MM_DD: {
$concat: [
{ $substrCP: [ "$birthday", 3, 2 ] },
"/",
{ $substrCP: [ "$birthday", 0, 2 ] }
]
}
}
},
// Sort with the newly formatted birthday field
{
$sort: {
birthday_MM_DD: 1
}
},
// Remove the temporary birthday field
// (as it is no longer needed)
{
$unset:
"birthday_MM_DD"
},
]
db.collection.aggregate(pipeline)
结果按 birthday
正确排序 格式 的文档,正如预期的那样:
"18/05", "03/11", "04/11", "15/12"