Get a model with at least one relationship object in a mangoid

I have a Person object that has several companies. I would like to get a person from at least one company. Now i can get

Person.where(:company_ids.size => 1) 

This will return all people with one company. But I need something like

 Person.where(:company_ids.size.gte => 1) 

But it doesn't seem to work.

Decision:

sorry for all the troubles, but found out that with the previously created objects I did not have company_ids ... since I only added this later. I can get an invoice with the following:

 Person.where(:company_ids.exists => true).and("this.company_ids.length > 0") 

Thank you all for your help.

+4
source share
3 answers

You checked the execution as

Person.where("this.company_ids >=1")

+3
source

I assume company_ids is an array field in a personal document

I am afraid that there is no way to indicate the conditions in size. But there is a workaround using javascript $, where the expression

  db.person.find({$where: '(this.company_ids.length > 0)'}) 

I am not sure how to pass this expression to mongoid.

EDIT

Yes, you can do it with a mangoid too

 Person.where("$where" => 'this.company_ids.length >0;' ) 
+4
source

You must be able to:

Person.where("this.company_ids.length > 3")

+4
source

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


All Articles