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.

Community
  • 1
  • 1
Huy
  • 10,806
  • 13
  • 55
  • 99

1 Answers1

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.

Huy
  • 10,806
  • 13
  • 55
  • 99