I'm using Mongoid to connect to mongodb and need help with a query.
I have a Parent model where each parent and child has a name.
Class Parent
field: :name
field: :child_name
end
I could break the child out into another model and/or embed it, but my DB needs are simple. I want to query all the documents where the child name is the same as the parent name. (e.g. Father is Jeff and son is also Jeff).
Tried the following, but it doesn't work.
parent = Parent.where(name: :child_name)
Not sure how to do it with Mongoid. Any help is much appreciated
If you provide a string to Mongoid's where()
, it assumes you're using JavaScript, and triggers MongoDB's native $where
, which is what you need:
Parent.where("this.name == this.child_name")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With