Py学习  »  Manjeet Thakur  »  全部回复
回复总数  1
5 年前
回复了 Manjeet Thakur 创建的主题 » MongoDB中limit()和$limit之间的差异
limit()

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

$limit 

当一个关键字以dollar开头时,它意味着operator $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)

两人都做同样的事情,结果有限