0

これらのquestionsドキュメントは mongodb にあり、複数のanswersドキュメントを埋め込むことができます。私はレールアプリでモンゴイドを使用しています。

{
  "_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 564297f441021327d3000001user_id ==564297f441021327d3000001 type == text || status == active

上記の例では、私の条件を使用すると、すべての質問にこれらの条件に一致する埋め込みドキュメントが 1 つあるため、上記の 3 つの質問ドキュメントがすべて返されます。

私はこのようにmongoidでクエリを書いてみましたが、もちろん、or私が使用できるようなものがないのでうまくいきません.

questions.where(asked_to: '564297f441021327d3000001').and("answer.user_id": "564297f441021327d3000001").and("answer.status": "active" or "answer.type": "text" )

では、どうすれば私が探しているものを達成できますか?

4

0 に答える 0