我有一个像这样的ObjectId数组
// Post Schema
{
_id: ObjectId('..'),
categories: [ObjectId('...'),ObjectId('...'),ObjectId('...')...]
}
// Category Schema
{
_id: ObjectId('..'),
name: '...',
...
}
// query
Post.aggregate()
.match({ _id: targetPostId })
.lookup({
from: 'categories',
localField: 'categories',
foreignField: '_id',
as: 'categories',
})
而且当我通过聚合查找运行mongo查询时,我注意到该Array的顺序与原始顺序不同。
使用查找时如何保持原始数组的顺序?
您可以将$lookup
与自定义$lookup
一起使用>
pipeline