I have a collection and the example datas are like this,
{
L:”images”,
K:”asdd”
}
{
L:”images”,
K:”asdd”
}
{
L:”images”,
K:”asdd”
}
{
L:”images”,
K:”asdd”
}
{
L:”images”,
K:”asdd”
}
{
L:”images”,
K:”asdd”
}
{
L:”images”,
K:”asdd”
}
{
L:”images”,
K:”asdd”
}
{
L:”images”,
K:”asdd”
}
{
L:”images”,
K:”asdd”
}
{
L:”images”,
K:”asdd”
}
{
L:”images”,
K:”asdd”
}
{
L:”images”,
K:”asdd”
}
and other fields are,{L:”cars”,K:”asdff”},{L:”table”,K:”asgeg”} those fields have at least 20 documents too, what I want as a result is like this
{
L:”images”,
K:”asdd”
}
{
L:”images”,
K:”asdd”
}
{
L:”cars”,
K:”asdd”
}
{
L:”cars”,
K:”asdd”
}
{
L:”table”,
K:”asdd”
}
{
L:”table”,
K:”asdd”
}
I want to get the documents according to their L field, but I want to limit every field’s result to two and I have no idea how to manage this, thank you for any reply 🙂
Given your schema, I don’t think there is a way, in a single query, to do what you want. I would recommend a PHP script with a simple foreach loop, or similar, to extract the values you want since you tagged this one as PHP.
Of course, you can just do it with 3 separate queries, one for each L value, with a limit of 2. I assume that we are really talking about a lot of distinct values and not just the three listed.
I’m not a PHP guy, so I can’t help you out there, but you can use the JavaScript capabilities of the shell similarly like this (I loaded your sample data into a collection called foo and it had an _id field added automatically):
I used distinct to generate the three values but you could easily just pass it in as an array on the first line like so:
Either way, the function gave me the following output: