在我的前端中,在对用户进行身份验证之后,我有以下代码可以工作:..
...
.then(authedUser =>
db
.collection('comments')
.find({}, { limit: 1 })
.asArray()
)
.then(doc => console.log('doc:', doc)); // 1 doc returned as array, yes!但是,以下代码不起作用:。
...
.then(authedUser =>
db
.collection('comments')
.find({})
.limit(1)
.asArray()
)
.then(doc => console.log('doc:', doc)); // error inside Promise, limit is not a function...我能知道为什么吗?我知道limit()是一个游标方法,而$limit是一个聚合阶段,所以现在我有点困惑了。
发布于 2018-11-29 13:43:11
这在文档中有点混乱,因为第二个可以在Stitch函数中工作,但在使用SDK时不能工作。第一种是在SDK中实现的正确方法。在SDK中,读操作没有修饰符,这意味着您不能在find()上调用.limit()。
这是您正在执行的操作的docs。希望这能有所帮助!
发布于 2018-11-29 14:07:19
limit()当关键字以括号结尾时,表示调用了一个方法
$limit 当关键字以美元开头时,表示运算符$limit operator
$limit仅适用于聚合
现在根据你的问题
.then(authedUser =>
db
.collection('comments')
.find({}, { limit: 1 })
.asArray()
)
.then(doc => console.log('doc:', doc));在这里,您将option作为3rd参数中的限制进行传递,其中1表示为true
在第二个代码中
.then(authedUser =>
db
.collection('comments')
.find({})
.limit(1)
.exec(function(err, result) {
// Do here as a array
return new Promise((resolve, reject) => {})
});
)
.then(doc => console.log('doc:', doc));您可以将limit方法作为cascading style(Chaining Methods)调用
两者都在做同样的事情,限制了结果
https://stackoverflow.com/questions/53531832
复制相似问题