嗨,我有两个集合,彼此没有引用,我想把它们合并在一个集合,然后我做分页。
但是它不起作用,我只在page为1时得到结果而对于其他页面,我只得到一个空数组
const search = async (skip, limit, query) => {
const regex = new RegExp(`^${query}`, 'i')
const cursor = await db.collection('cats').aggregate([
{ $unionWith: {
coll: 'dogs',
pipeline: [{ $project: { 'breeds': false } }]
}
},
{ $match: { $or: [
{ 'dog_name': { $regex: regex } },
{'cat_name': { $regex: regex } }
]}},
{ $limit: limit },
{ $skip: skip }
]).toArray()
return cursor
}
const result1 = search(0, 10, 'Mi') // This works
const result2 = search(10, 10, 'Mi') // This doesn't page(2)
const result2 = search(20, 10, 'Mi') // This doesn't page(3)
试试这个:
const cursor = await db.collection('cats').aggregate([
{ $unionWith: {
coll: 'dogs',
pipeline: [{ $project: { 'breeds': false } }]
}
},
{ $match: { $or: [
{ 'dog_name': { $regex: regex } },
{'cat_name': { $regex: regex } }
]}},
{ $skip: skip } ,
{ $limit: limit }
]).toArray()
你在跳过之前限制了,也许这就是问题所在。