0

在我的前端和验证用户后,我有以下代码有效:..

...
.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 是一个聚合阶段,所以现在我有点困惑。

4

2 回答 2

0

这在文档中有点令人困惑,因为第二个可以在 Stitch 函数中使用,但在使用 SDK 时不起作用。第一个是从 SDK 执行此操作的正确方法。在 SDK 中,读取操作没有修饰符,这意味着您不能调用.limit().find()

这是您正在做的事情的文档。希望这可以帮助!

于 2018-11-29T05:43:10.567 回答
-2
limit()

当关键字以括号结尾时,表示调用了一个方法

$limit 

当关键字以美元开头时,表示运算符$limit 运算符

$limit 仅适用于聚合

现在根据你的问题

.then(authedUser =>
        db
          .collection('comments')
          .find({}, { limit: 1 })
          .asArray()
      )
      .then(doc => console.log('doc:', doc));

在这里,您将 limit 作为optionin3rd参数传递,其中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)

并且都做同样的事情限制了结果

于 2018-11-29T06:07:18.927 回答