user3675188
user3675188

Reputation: 7409

How could I convert the mongoid query into real mongo query js syntax

I go this query on the console, however I could not simply copy and paste it to execute it in mongo shell.

Is there any method to convert the mongoid DSL into real mongo query statement.

Thanks

database=test collection=flights selector={"$query"=>{"from"=>{:$in=>["TPE"]}, "to"=>{:$in=>["HND", "NRT", "NRT|TYO"]}, "flight_date"=>{"$gte"=>2016-03-15 00:00:00 UTC, "$lt"=>2016-03-16 00:00:00 UTC}, "flight_no"=>{:$exists=>true, :$ne=>nil}, "updated_at"=>{"$gte"=>2016-03-02 09:16:13 UTC}}, "$orderby"=>{"departure_at"=>1}}

Upvotes: 3

Views: 101

Answers (1)

kuadrosx
kuadrosx

Reputation: 411

you can do something like this:

module Mongoid
    class Criteria
        def to_shell
            "db.#{collection.name}.find(#{selector.to_json})"
        end
    end
end

Flight.where({:from.in => ["TPE"], :to.in => ["HND", "NRT", "NRT|TYO"]}).to_shell

Upvotes: 1

Related Questions