0

I am wanting to write some ruby to iterate through documents in a collection in a MongoDB.

My data has the schema:

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

I firstly want to count each time userId = 1 is present in the whole collection, and if less than 5 discard them all.

I'm really unsure how to tackle this, so any advice would be great.

1 Answer 1

1

You would need to count the number of documents which have userId = 1 through the count() method. Thus from the shell (command-line), you can do the following:

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

You'll then have to do something similar with Ruby, but it should be pretty straightforward. Refer to the documentation on the Ruby driver for this:

  1. Get a count of matching documents in the collection.
  2. Remove documents from the collection
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tip, I should be able to convert this into a ruby block.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.