我对mongo很新,我正在尝试研究如何对查询结果应用复杂的排序。我收集了一些帖子,其中包含“用户”或“提升”类型,并且都有创建日期。我需要做的是按创建日期排序列表,但确保两个最近推荐的帖子位于列表的顶部,无论它们何时创建。如果可能的话,我更愿意在mongo中使用现有的分页解决方案。
查找前两个推广帖子很简单:
db.example.find({type:“pro”})。sort({create_dt:1})。limitit(2)
但是,我是否需要对所有重新发布的帖子(不包括原来的前两个)进行聚合,并以某种方式加入两组结果?或者有没有办法动态标记前两个提升的帖子,然后使用标签订购整个列表?
感谢。
答案 0 :(得分:1)
db.restaurants.aggregate(
{
$match: {/* for optimization only: any potentially reasonable filters to speed up the query */}
},
{
$sort:
{
"type": 1, // 'promoted' first and then 'user' posts
"create_dt": -1 // the latest posts first
}
},
{
$group:
{
_id: "$type", // two groups, one for 'promoted' posts & one for type 'user'
items: { $push: "$$ROOT" } // remember the entire document
}
},
{
$project:
{
"items":
{
$zip: // we use $zip to reduce the number of items per group...
{
inputs:
[
{
$cond:
[
{ $eq: ["$_id", "promoted"] }, // ...depending on the type...
[1,2], //...to 2 items in the case of 'promoted' type posts
{ $range: [ 1, 100, 1 ] } //...and to 100 items - change if needed - for 'user' type posts
]
}
,
"$items"
]
}
}
}
}
,
{
$unwind:
{
path: "$items", // first level of unwinds --> we get an array output still from the $zip operation
}
},
{
$unwind:
{
path: "$items", // second level of unwinds - we flatten everything to the lowest level
}
},
{
$match:
{
"items": { $type: 3 } // get rid of the index element that got created in the $zip stage, only keep objects
}
},
{
$replaceRoot: { newRoot: "$items" } // make sure we get a nice list of posts back
}
)