Query Mongo for multiple key value pair combinations in array field

时间:2016-02-12 20:51:03

标签: mongodb mongodb-query

I am trying to query for Mongo documents based on 1 many key value pairs over a large collection of documents.

I am storing the values in an array called descriptors. I cannot figure out to write the query to consistently return results.

Basically I am trying to write the following SQL query in Mongo

for (int i = 1; i <= 5; i++)
{
    cout << "Enter 1 to play, or 2 to exit: ";
    cin >> chose;
    cout << endl;
    if (chose == 1)
    {
        ResultPlayer = RollDice();
        ResultComputer = RollDice();
        DisplayDice(ResultPlayer, ResultComputer, i); 
        cout << endl;
        LoseRollDice(ResultPlayer, ResultComputer, i);
        // if the player lose, how to break after this function if the condition is true in it??
        WinRollDice(ResultPlayer, ResultComputer, i, SumPlayer, SumComputer);
    }
    else break;
}

I have tried the following:

 select * from descriptors where 
    (id=123 and value="Latest based on new infor")
  or 
    (id=3221 and value="Latest new info")

Here are sample documents.

db.collection.find({$or: [
       {  descriptors:
         {
          '$elemMatch':
            {
               id: 123,
               value: 'Latest based on new infor',

            }
         }
       },
       {  descriptors:
        {
          '$elemMatch':
            {
               id: 3221,
               value: 'Latest new info',

            }
        }
       }
    ]
}

Any help would be appreciated, gsvi

0 个答案:

没有答案