我在mongodb中有这些questions
文档,并且可以在其中嵌入多个answers
文档。我在rails app中使用mongoid。
{
"_id" : ObjectId("5642993541021327d3000004"),
"asked_to" : [
"5642978841021327d3000000",
"564297f441021327d3000001"
],
"text": "What is your hobby?",
"answers": [
{
"_id" : ObjectId("56429baf41021327d3000005"),
"user_id": "5642978841021327d3000000",
"type": "text",
"text": "Playing soccer",
"status": "active"
},
{
"_id" : ObjectId("46429baf41021327d3000092"),
"user_id": "564297f441021327d3000001",
"type": "video",
"video_url": "http://www.myvideo.com/ewkrjwert7",
"status": "inactive"
}
]
}
{
"_id" : ObjectId("7642993541021327d30000022"),
"asked_to" : [
"9642978841021327d3000027",
"564297f441021327d3000001",
"994297f441021327d3000002"
],
"text": "What is your name?",
"answers": [
{
"_id" : ObjectId("26429baf41021327d3000004"),
"user_id": "564297f441021327d3000001",
"type": "text",
"text": "John abc",
"status": "inactive"
},
{
"_id" : ObjectId("86429baf41021327d3000091"),
"user_id": "9642978841021327d3000027",
"type": "text",
"text": "Mary Kay",
"status": "inactive"
},
{
"_id" : ObjectId("86429baf41021327d3000091"),
"user_id": "994297f441021327d3000002",
"type": "video",
"video_url": "http://www.myvideo.com/khgfdiuweo",
"status": "active"
}
]
}
{
"_id" : ObjectId("5642993541021327d3000004"),
"asked_to" : [
"5642978841021327d3000000",
"564297f441021327d3000001"
],
"text": "Where do you live?",
"answers": [
{
"_id" : ObjectId("56429baf41021327d3000005"),
"user_id": "5642978841021327d3000000",
"type": "video",
"text": "Playing soccer",
"status": "active"
},
{
"_id" : ObjectId("46429baf41021327d3000092"),
"user_id": "564297f441021327d3000001",
"type": "video",
"video_url": "http://www.myvideo.com/ewkrjwert7",
"status": "inactive"
}
]
}
我想获取asked_to includes 564297f441021327d3000001
所有问题文档,其中任何嵌入式答案文档都符合此条件user_id ==564297f441021327d3000001
type == text || status == active
所以在上面的例子中,使用我的条件,它应该返回我以上三个问题文档,因为每个问题,有一个符合这些条件的嵌入式文档
我尝试在这样的mongoid中编写查询,但当然它不会工作,因为我没有这样的or
。:
questions.where(asked_to: '564297f441021327d3000001').and("answer.user_id": "564297f441021327d3000001").and("answer.status": "active" or "answer.type": "text" )
那么我怎样才能实现我的目标呢?