【问题标题】:Difference between limit() and $limit in MongodbMongodb中limit()和$limit的区别
【发布时间】:2018-11-30 00:27:30
【问题描述】:

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

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

【问题讨论】:

  • asArray() 对于 SDK 是正确的。这是使用缝合,所以它与驱动程序不同。
  • 是的,我正在使用并参考 Stitch SDK。

标签: mongodb mongodb-stitch


【解决方案1】:

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

这里是docs,您正在做的事情。希望这可以帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 2015-01-25
    • 2018-08-18
    • 1970-01-01
    • 2012-06-19
    • 2021-11-29
    相关资源
    最近更新 更多