5

I have the follow raw mongo query that I want to execute directly in my Rails controller:

 db.user_places.update({place_id: destroy_model.id}, {$set: {place_id: keep_model.id}}, false, true)

For sequel, I was using ActiveRecord::Base.connection.execute(sql) to execute my sql queries.

What's the equivalent way of doing this using MongoDB/mongoid gem? I've tried following How to fire raw MongoDB queries directly in Ruby, but I'm not using MongoMapper.

Thank you.

Edit: My solution to this problem was to just run the following from Mongoid documentation:

UserPlace.where(place_id: destroy_model.id).update_all(place_id: keep_model.id)

This will generate the raw mongo query above and execute it in ActiveRecord fashion.

1 Answer 1

0

I found a way to do it without have to establish a connection with Mongo console. Using mongoid, you can just use the update_all to generate the same query.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.