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

  • Home
  • SEARCH
  • 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 8687603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:09:27+00:00 2026-06-12T23:09:27+00:00

If I have a collection similar to: [ { test: [ { a: 1,

  • 0

If I have a collection similar to:

[
    { "test": [ { "a": 1, "b": 2 }, { "a": 10, "b": 1 } ] },
    { "test": [ { "a": 5, "b": 1 }, { "a": 14, "b": 2 } ] },
    ...
]

How do I obtain only a subset of data consisting of the a values when b is 2? In SQL, this would be something similar to:

SELECT test.a FROM collection WHERE test.b = 2

I do understand that I can limit what data I get with something like:

collection.find({ }, { "test.a": 1 })

But that returns all the a values. How can I limit it so that it returns only the values in which b is 2 (the WHERE test.b = 2 part of the SQL equivalent)?

  • 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-12T23:09:29+00:00Added an answer on June 12, 2026 at 11:09 pm

    You can do this by adding a selector object as the first parameter of your find call and using the $elemMatch projection operator:

    collection.find({ 'test.b': 2 }, { test: { $elemMatch: { b: 2 } }, 'test.a': 1 })
    

    But this will only return the first test array element per-doc where b is 2. You would need to use the aggregation framework if there can be multiple b:2 elements in your test arrays.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection similar to: Public Class MyCollection Inherits ObservableCollection(Of MyCollection) Private _Name
I have Collection List<Car> . How to compare each item from this collection with
I have this NSMutableArray which is a collection of objects that are being moved
I have a test db on godaddy, I have had a similar issue before
i have a mongo collection like this: { A2_AboutMe: , A2_Attributes: |av|nv|, A2_Birthday: ,
I would like to have an infinite/endless scroll data rendering from a JSON feed.
I have a model class similar to the following: using System; using System.Collections.Generic; using
Lets say I have collection of documents with specified longitude and latitude. type is
I'm planning to have collection of items stored in a TCollection. Each item will
for example I have collection foo of documents like that: {tag_cloud:[{value:games, count:10}, {value:girls, count:500}]}

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.