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

mongodb ruby驱动程序参数计数操作

  •  1
  • razenha  · 技术社区  · 14 年前

    我正试图使用Ruby驱动程序使用count()Mongodb特性(db.collection_name.count({data:value})。我尝试使用de collection.count方法,但它不接受任何参数。

    我检查了collection.count()方法文档,它只返回集合中对象的总数,您无法向此方法传递“filter”参数。

    3 回复  |  直到 8 年前
        1
  •  3
  •   Gates VP    14 年前

    是否可以以其他方式将count()Mongodb特性与过滤器参数一起使用?

    在shell(命令行)中,可以执行以下操作:

    db.collection.find({ data : value}).count()

    显然,您必须对Ruby做一些类似的事情,但是应该非常简单。

        2
  •  2
  •   Gîanfranco P.    11 年前

    # actual number of records
    DB["posts"].find({"author" => "john"}).to_a.length
    => 48
    # *total* number of documents in the collection (the query matching is ignored)
    DB["posts"].count({"author" => "john"})
    => 1431
    # more efficient way (using the count server-side command)
    DB["posts"].find({"author" => "john"}).count
    => 1431
    
        3
  •  0
  •   xavdid    10 年前

    实际上可以使用 .count() 正如您在命令行中所做的那样,它只是文档记录得很差。

    Mongo命令行:

    db.User.find({"emails.5": {"$exists": true}}).count()

    Mongo Ruby驱动程序:

    db.collection('User').count({:query => {"emails.5" => {"$exists" => true}}})

    这个和其他 选项是文档 here

    推荐文章