对MongoDB中的子文档进行排序
有没有办法在mongo查询中对子文档进行排序 例如:对MongoDB中的子文档进行排序,mongodb,sorting,nested,Mongodb,Sorting,Nested,有没有办法在mongo查询中对子文档进行排序 例如: { "_id" : ObjectId("4c69d19532f73ad544000001"), "content" : "blah blah blah", "comments" : [ {"author": "jim", "content":"comment content 1", "date" : "07-24-1995"}, {"author": "joe", "content":"comment conten
{
"_id" : ObjectId("4c69d19532f73ad544000001"),
"content" : "blah blah blah",
"comments" : [
{"author": "jim", "content":"comment content 1", "date" : "07-24-1995"},
{"author": "joe", "content":"comment content 2", "date" : "07-24-1996"},
{"author": "amy", "content":"comment content 3", "date" : "09-10-1999"}
]
}
{
"_id" : ObjectId("4c69d19532f73ad544000002"),
"content" : "blah blah blah",
"comments" : [
{"author": "jim", "content":"comment content 1", "date" : "07-24-1995"},
{"author": "joe", "content":"comment content 2", "date" : "07-24-1996"},
{"author": "amy", "content":"comment content 3", "date" : "07-24-1997"}
]
}
我希望这些文件按我决定的方式排序,然后在我的文件中按日期的相反顺序或任何其他我想要的排序排序
mongoDB有可能吗 我想你做不到。我在上提出了同样的问题。不可能直接从MongoDB中提取文档,但是当您提取文档时,您可以使用您的语言拥有的任何本机排序方法对数组进行排序,就像它是一个对象数组一样。这就是我在MongoDB博客上发表评论的方式 PHP代码
/*接受注释数组并将其转换为本机对象*/
公共静态函数loadMany($pcomments)
{
$comments=array();
如果(isset($pcomments)和计数($pcomments)>0)
{
foreach($pcomments as$key=>$comment)
{
$comment['data']['index']=$key;
$comments[]=Comment::loadOne($Comment['data']);
}
}
usort($comments,“comment_compare”);
返回$comments;
}
/*比较注释时间戳*/
函数注释\u比较($a,$b)
{
如果($a->时间戳->秒==$b->时间戳->秒)
{
返回0;
}
返回($a->timestamp->sec<$b->timestamp->sec)?-1:1;
}
在mongo 2.4版或更高版本中,您可以在更新时使用$sort。
新的修改器允许您对子文档进行排序
更多信息请阅读本页,
从
Mongo4.4
开始,聚合操作符允许应用自定义javascript函数来实现MongoDB查询语言不支持的行为
例如,要按对象的一个字段对对象数组进行排序,请执行以下操作:
// {
// "content": "blah blah blah",
// "comments": [
// { "author": "jim", "content": "comment content 1", "date": "07-24-1995" },
// { "author": "joe", "content": "comment content 2", "date": "07-24-1996" },
// { "author": "amy", "content": "comment content 3", "date": "09-10-1999" }
// ]
// }
db.collection.aggregate(
{ $sort: { content: 1 } }, // or whatever sort order
{ $set:
{ "comments":
{ $function: {
body: function(comments) {
return comments.sort((a, b) => a.date < b.date);
},
args: ["$comments"],
lang: "js"
}}
}
}
)
// {
// "content": "blah blah blah",
// "comments": [
// { "author": "amy", "content": "comment content 3", "date": "09-10-1999" },
// { "author": "joe", "content": "comment content 2", "date": "07-24-1996" },
// { "author": "jim", "content": "comment content 1", "date": "07-24-1995" }
// ]
// }
//{
//“内容”:“诸如此类”,
//“评论”:[
//{“作者”:“吉姆”,“内容”:“评论内容1”,“日期”:“1995年7月24日”},
//{“作者”:“乔”,“内容”:“评论内容2”,“日期”:“1996年7月24日”},
//{“作者”:“艾米”,“内容”:“评论内容3”,“日期”:“1999年10月9日”}
// ]
// }
db.collection.aggregate(
{$sort:{content:1}},//或任何排序顺序
{$set:
{“评论”:
{$function:{
机构:职能(评论){
返回注释.sort((a,b)=>a.date
$function
采用3个参数:
,它是要应用的函数,其参数是要修改的数组body
,其中包含args
函数体作为参数的记录字段。在我们的例子中,
“$comments”
,这是编写lang
函数体的语言。目前只有
可用js
- 按顺序使用
$unwind
$sort
$group
阵列专用时
- 解构数组
注释
$addFields
若要为日期添加新字段,字段为字符串,我们需要将其转换为ISO日期以进行适当排序,若要将字符串日期转换为ISO日期,请使用
comments.dateISO
按降序(-1)排列
- 通过
\u id
,并使用$push
重新构造注释
数组对象和其他字段仍然使用$first
但是$sort修饰符似乎处理$push,例如,如果我想在执行$inc时按投票对其进行排序,这似乎是不可能的
// {
// "content": "blah blah blah",
// "comments": [
// { "author": "jim", "content": "comment content 1", "date": "07-24-1995" },
// { "author": "joe", "content": "comment content 2", "date": "07-24-1996" },
// { "author": "amy", "content": "comment content 3", "date": "09-10-1999" }
// ]
// }
db.collection.aggregate(
{ $sort: { content: 1 } }, // or whatever sort order
{ $set:
{ "comments":
{ $function: {
body: function(comments) {
return comments.sort((a, b) => a.date < b.date);
},
args: ["$comments"],
lang: "js"
}}
}
}
)
// {
// "content": "blah blah blah",
// "comments": [
// { "author": "amy", "content": "comment content 3", "date": "09-10-1999" },
// { "author": "joe", "content": "comment content 2", "date": "07-24-1996" },
// { "author": "jim", "content": "comment content 1", "date": "07-24-1995" }
// ]
// }
db.collection.aggregate([
{ $unwind: "$comments" },
{
$addFields: {
"comments.dateISO": {
$dateFromString: {
dateString: "$comments.date",
format: "%m-%d-%Y"
}
}
}
},
{ $sort: { "comments.dateISO": -1 } },
{
$group: {
_id: "$_id",
comment: { $first: "$comment" },
comments: { $push: "$comments" }
}
}
])