社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  MongoDB

MongoDB中limit()和$limit之间的差异

Mauricio Maroto • 6 年前 • 965 次点击  

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

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

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40226
文章 [ 2 ]  |  最新文章 6 年前
Manjeet Thakur
Reply   •   1 楼
Manjeet Thakur    7 年前
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)

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

haley
Reply   •   2 楼
haley    7 年前

这在文档中有点混乱,因为第二个函数可以在Stitch函数中工作,但在使用sdk时不起作用。第一种方法是从sdk中正确地执行它。在sdk中,读取操作没有修饰符,这意味着您不能调用 .limit() 在一 find() .

这里是 docs 因为你在做什么。希望这有帮助!