Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoid Object.all seems to return a lot of []

Why doesn't this work:

ruby-1.8.7-p249 > List.create :search_terms => 'foo'
 => #<List _id: 4c9044a02249c7a5e2000001, search_terms: "foo", user_id: nil> 

ruby-1.8.7-p249 > List.all
 => #<Mongoid::Criteria:0x1030dea90 @klass=List, @documents=[], @selector={}, @options={}> 

ruby-1.8.7-p249 > List.all.documents
 => [] 
like image 346
ian Avatar asked Sep 15 '10 04:09

ian


People also ask

How do I fetch an array of objects in MongoDB?

To search the array of object in MongoDB, you can use $elemMatch operator. This operator allows us to search for more than one component from an array object.

How do I filter an array in MongoDB?

Filter MongoDB Array Element Using $Filter Operator This operator uses three variables: input – This represents the array that we want to extract. cond – This represents the set of conditions that must be met. as – This optional field contains a name for the variable that represent each element of the input array.

How do I select a specific object in MongoDB?

The easiest way is to just filter the shapes in the client. If you really need the correct output directly from MongoDB, you can use a map-reduce to filter the shapes. Save this answer.

How do you check if a value exists in an array in mongoose?

In your case you could try: Person. find({ members: { $elemMatch: { id: id1 } } });


1 Answers

I think what you're looking for is:

List.all.to_a
like image 156
clemensp Avatar answered Sep 24 '22 16:09

clemensp