Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8849205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:35:26+00:00 2026-06-14T12:35:26+00:00

I have a collection and the example datas are like this, { L:images, K:asdd

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:35:27+00:00Added an answer on June 14, 2026 at 12:35 pm

    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):

    db.foo.distinct("L").forEach(function(key) {
        db.foo.find({"L" : key}, {_id : 0}).limit(2).forEach(
            function (value) {
                printjson(value);
            }
        )
    })
    

    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:

    ['images', 'cars', 'table'].forEach(function(key) {
    

    Either way, the function gave me the following output:

    { "L" : "images", "K" : "asdd" }
    { "L" : "images", "K" : "asdd" }
    { "L" : "cars", "K" : "asdd" }
    { "L" : "cars", "K" : "asdd" }
    { "L" : "table", "K" : "asdd" }
    { "L" : "table", "K" : "asdd" }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}
I'd like to have a class A with a (for example) SortedList collection SrtdLst
I have a collection of objects I bind to a Listview like this: if
Say I have a collection of users like this:- { _id : 1234, Name
I have Backbone.js collection that holds (for example) 30 items. I want to pass
Can I contain two different types in a collection? For example, can I have
I have this simple example: using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program
I have Collection List<Car> . How to compare each item from this collection with
i have a bunch of data collection , example : 1.00 3 4 1.00
I have a set of classes something like this: abstract class CollectionAbs implements Iterator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.