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

mongodb ruby驱动程序的参数计数操作,ruby,mongodb,nosql,Ruby,Mongodb,Nosql,我尝试使用Ruby驱动程序使用count()Mongodb特性(db.collection\u name.count({data:value}),我尝试使用de collection.count方法,但它不接受任何参数 我检查了collection.count()方法docs,它只返回集合中对象的总数,您无法向该方法传递“filter”参数 是否可以以其他方式将count()Mongodb功能与过滤器参数一起使用 是否可以以其他方式将count()Mongodb功能与过滤器参数一起使用 在she

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

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

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

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

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

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

显然,您必须使用Ruby做一些类似的事情,但它应该非常简单

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

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

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


显然,你必须用Ruby做一些类似的事情,但它应该非常简单。

对于那些只想使用Ruby驱动程序得到答案的人:

# 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

对于那些只想使用ruby驱动程序获得答案的人:

# 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

实际上可以像在命令行上那样使用
.count()
,只是文档记录得很差

Mongo命令行:

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

Mongo Ruby驱动程序:

# 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
db.collection('User').count({:query=>{“emails.5”=>{“$exists”=>true}}})


此选项和其他
.count()
选项都是文档。

实际上可以像在命令行上那样使用
.count()
,只是文档记录得很差

Mongo命令行:

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

Mongo Ruby驱动程序:

# 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
db.collection('User').count({:query=>{“emails.5”=>{“$exists”=>true}}})


这个和其他
.count()
选项都是文档。

不,从shell中我不需要使用find()然后count(),我可以使用count()和参数。我不想将所有数据拖到内存中只是为了获得给定查询的对象数。好的,.find()方法只返回一个光标,它不会返回所有数据。当您执行count()时它应该循环遍历数据,而不实际加载数据并将其推过网络。不,从shell中,我不需要使用find()然后使用count(),我可以使用count()和参数。我不想将所有数据拖到内存中,只是为了得到给定查询的对象数。好的,“.find()”方法只返回一个游标,而不返回所有数据。当您执行count()时,它应该循环遍历数据,而不实际加载数据并将其推过导线。