Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I query for item with non-empty array with mongoid?

I have the following code that works as expected:

Mongoid::Criteria.new(Question).where(:related_question_ids.size => 0)

However, I would like to perform the query to return questions with related_questions array greater than 0. For example,

Mongoid::Criteria.new(Question).where(:related_question_ids.size.gte => 0)

Is there a way to do this with mongoid or mongodb?

like image 438
Kamilski81 Avatar asked Aug 28 '12 20:08

Kamilski81


2 Answers

This query search if exists any object in the related_question_ids[0] field

Using js shell

db.questions.find("related_question_ids.0": {exists => true} )

Using mongoid

Mongoid::Criteria.new(Question).where(:"related_question_ids.0".exists => true)

You can search greater any size

Mongoid::Criteria.new(Question).where(:"related_question_ids.3".exists =>true)

This solve your problem

like image 177
miguel savignano Avatar answered Sep 29 '22 01:09

miguel savignano


You can use the $size operator to query by array sizes. Consider the following example using the JS shell:

> db.foo.drop()
> db.foo.insert({_id: 1, x:[1,2]});
> db.foo.insert({_id: 2, x:[]});
> db.foo.insert({_id: 3, x:3});

> db.foo.find({x: {$size: 0}})
{ "_id" : 2, "x" : [ ] }

> db.foo.find({x: {$size: 1}})

> db.foo.find({x: {$size: 2}})
{ "_id" : 1, "x" : [ 1, 2 ] }

> db.foo.find({x: {$not: {$size: 2}}})
{ "_id" : 2, "x" : [ ] }
{ "_id" : 3, "x" : 3 }

> db.foo.find({x: {$not: {$size: 0}}})
{ "_id" : 1, "x" : [ 1, 2 ] }
{ "_id" : 3, "x" : 3 }

I'm not familiar with Mongoid, but I found an example using $size in this documentation.

Two caveats with $size are that it cannot utilize an index (other parts of the query certainly can) and it cannot be used in range queries. If you don't mind the additional bookkeeping, a viable option is to store the array's size in a separate field (likely indexed) and query on that any way you like.

like image 42
jmikola Avatar answered Sep 29 '22 02:09

jmikola