代码之家  ›  专栏  ›  技术社区  ›  Federico

mongodb中db.mydb.find({tag:'java'}.count()和db.mydb.count({tag:'java'})的区别是什么

  •  2
  • Federico  · 技术社区  · 15 年前

    我需要在蒙多布快速计算出大量符合标准的文件(30米)

    一个比另一个快?

    谢谢,

    费德里科。

    2 回复  |  直到 15 年前
        1
  •  6
  •   brianz    15 年前

    下面是一个小技巧,看看MongoDB命令在幕后的作用:

    > 
    > db.mydb.count
    function (x) {
        return this.find(x).count();
    }
    >
    > db.mydb.find().count
    function (applySkipLimit) {
        var cmd = {count:this._collection.getName()};
        if (this._query) {
            if (this._special) {
                cmd.query = this._query.query;
            } else {
                cmd.query = this._query;
            }
        }
        cmd.fields = this._fields || {};
        if (applySkipLimit) {
            if (this._limit) {
                cmd.limit = this._limit;
            }
            if (this._skip) {
                cmd.skip = this._skip;
            }
        }
        var res = this._db.runCommand(cmd);
        if (res && res.n != null) {
            return res.n;
        }
        throw "count failed: " + tojson(res);
    }
    

    所以是的,你可以看到 collection.count collection.find().count

        2
  •  2
  •   geofflane    15 年前

    他们是一样的。MongoDB将所有查询参数视为单个对象。在本例中,count是一个“cursor方法”,这意味着它是在服务器端执行的。见: http://www.mongodb.org/display/DOCS/Querying