0

Below is my variable.

var list = [sun,moon,earth,wind,air];

db.nature.find({"name":{"$in":list}}).limit(2)

The thing is each element in the list array contains more than 5 search results..

For example: the keyword 'sun' has 5 search results, the keyword 'moon' has 5 search results the keyword 'earth' has 5 search results the keyword 'wind' has 5 search results the keyword 'air' has 5 search results... i need to limit each search results by 2..(i.e)i need to have the first two search list of every element in the list array.what can be done? The wind element in the list contains the search result as below.

 {
 "_id": "59229b31a8de7a8323c1e92a",
        "name": "wind",
        "class":"1"
        }
        {
 "_id": "59229b31a8de7a8323c1e93a",
        "name": "wind",
        "class":"2"
        }
        {
 "_id": "59229b31a8de7a8323c1e94a",
        "name": "wind",
        "class":"3"
        }
        {
 "_id": "59229b31a8de7a8323c1e95a",
        "name": "wind",
        "class":"4"
        }
        {
 "_id": "59229b31a8de7a8323c1e62a",
        "name": "wind",
        "class":"5"
        }

But what i need is i need only the first two search result of every each element likewise..

Community
  • 1
  • 1
Jagadeesh
  • 1,967
  • 8
  • 24
  • 47
  • this is not verry clear. Can you add sample documents from the `nature` collection and the expected output – felix May 22 '17 at 10:29
  • updated check now.. – Jagadeesh May 22 '17 at 10:35
  • @felix It's pretty clear if you read it "2 sun, 2 moon" etc. It's a well known problem. For which the short answer is not really in one query. On "very small" result sets you can mangle this with the aggregation framework. But until [SERVER-9377](https://jira.mongodb.org/browse/SERVER-9377) is resolved, it's not really practical for real world data. The best approach for now would be to run multiple queries in parallel and combine the results in your client code. – Neil Lunn May 22 '17 at 10:37
  • and so this cant be done in one query?@neil Lunn – Jagadeesh May 22 '17 at 10:40
  • @neil Lunn can you answer the query with code? – Jagadeesh May 22 '17 at 10:44
  • Already did [mongodb group values by multiple fields](http://stackoverflow.com/questions/22932364/mongodb-group-values-by-multiple-fields) – Neil Lunn May 22 '17 at 10:46

0 Answers0