【发布时间】:2017-09-17 01:34:13
【问题描述】:
我正在使用 $limit 进行 Mongoose/MongoDB .aggregate 查询。当我使用一个数字时,比如2,它可以正常工作。如果我设置像testNum = 2 这样的变量,然后执行{$limit: varNum},它工作正常。但如果我发送一个 REST 查询并尝试执行 $limit: req.body.show,它会说该值不是数字。
我可以通过console.log 看到该值是一个数字。管道中的其他查询不会抱怨没有给出数字。代码如下:
var show = req.query.show, // the number of items to show per page
page = req.query.page, // the current page being asked for
stream = req.params.stream, // the type of content to get
skip = ( page > 0 ? (( page - 1 ) * show ) : 0 ), // amount to skip
testNum = 3
console.log( show + " " + skip + " " + page )
Content.aggregate( [
{ $unwind: '$users' },
{ $group: {
_id: '$_id',
title: { $first: '$title' },
description: { $first: '$description' },
images: { $first: '$images' },
url: { $first: '$url' },
saveCount: { $sum: 1 } }
},
{ $sort: { saveCount: -1 } },
{ $skip: skip },
{ $limit: show }
] )
.exec()
这里的查询是?show=2&page=1。控制台输出为2 0 1。这是正确的。
完整的错误在这里:
{ [MongoError: exception: the limit must be specified as a number]
name: 'MongoError',
errmsg: 'exception: the limit must be specified as a number',
code: 15957,
ok: 0 }
【问题讨论】:
标签: node.js mongodb express mongoose