2015-10-04 32 views
0

我想编写一些ruby来遍历MongoDB中的集合中的文档。要从Mongo删除的Ruby代码

我的数据有模式:

"_id" : ObjectId("560ff830eeb4db07875b59b9"), 
"userId" : NumberInt(1), 
"movieId" : NumberInt(50), 
"rating" : 4.0, 
"timestamp" : NumberInt(1329753504) 

我首先要计算每一次用户id = 1是目前整个集合中,如果少于5丢弃它们。

我真的不确定如何解决这个问题,所以任何建议都会很棒。

回答

1

您需要通过count()方法来计算具有userId = 1的文档的数量。因此,从外壳(命令行),你可以做到以下几点:

var count = db.collection.find({ "userId": 1 }).count(); 
if (count < 5) db.collection.remove() 

然后您就必须做与红宝石类似的东西,但它应该是相当简单的。请参阅在Ruby驱动程序的文档此:

  1. Get a count of matching documents in the collection.
  2. Remove documents from the collection
+1

谢谢你的提示,我应该能够将其转换成一个红宝石块。 – Benirving92