1
votes

I'm very beginner of mongoid, so I apology for the basic question.

I'm looking for an mongoid statement which throw the mongoDB query like below:

db.mycollection.find({"status.user.name": {$in:["jack","mary"]}}

There are two documents in mycollection in mongoDB

{ "status": {"user: {:name : "jack"}} }
{ "status": {"user: {:name : "mary"}} }

I tried below queries with mongoid, but it did not fetch any document:

MyCollection.where("status.user.name" => {'$in' => ["jack","mary"]})
MyCollection.in("status.user.name" =>  ["jack","mary"])

update on 2013/05/04

I'm sorry. I did not grasp the actual situation when I submit the question. But now I understand what happened and solved the issue. I update the question for those struggling the same issue.

The problem came from my misunderstanding of "embedding" and "referencing".

In the above situation, user is embedded in mycollection. Then, the in method can fetch all of two documents.

MyCollection.in("status.user.name" => ["jack", "mary"]).size
=> 2

But, in my actual situation, mycollection just referenced the user collection.

mycollection collection
{ "status": {"user_id": xyzxyz} }
{ "status": {"user_id": abcabc} }
user collection
{ "_id":xyzxyz, "name": "jack" }
{ "_id":abcabc, "name": "mary" }

That's why I couldn't fetch any documents with where or in method. In my understanding, I can't fetch user documents via mycollection with only one query becaus MongoDB isn't a RDBMS and doesn't support such kind of relation query.

MyCollection.in("status.user.name" => ["jack", "mary"]).size
=> 0
1

1 Answers

0
votes

How about:

MyCollection.in(status.user.name: [ "jack", "mary" ]).find();