我questions
在 mongodb 中有这些文档,它可以answers
嵌入多个文档。我在 Rails 应用程序中使用 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" )
那么我怎样才能实现我想要的呢?