Mongoid query collection, where two columns are equal to each other

I am using Mongoid to connect to mongodb and you need help with the request.

I have a parent model where each parent and child has a name.

Class Parent field: :name field: :child_name end 

I could split the child into another model and / or build into it, but my database needs are simple. I want to request all documents where the child name matches the name of the parent. (for example, father is Jeff and son is Jeff).

Tried the following, but it does not work.

  parent = Parent.where(name: :child_name) 

I don't know how to do this with Mongoid. Any help is much appreciated

+4
source share
1 answer

If you point the line to Mongoid where() , it is assumed that you are using JavaScript and running MongoDB native $where , which is what you need:

Parent.where("this.name == this.child_name")

+6
source

Source: https://habr.com/ru/post/1499783/


All Articles