Skip to Main Content

MongoByte MongoDB Logo

Welcome to the new MongoDB Feedback Portal!

{Improvement: "Your idea"}
We’ve upgraded our system to better capture and act on your feedback.
Your feedback is meaningful and helps us build better products.

Status Planned
Categories Atlas Search
Created by Guest
Created on Jul 20, 2020

Return results from within arrays

I have a document that looks like this: { shoppingList: [ { name: "red hat"}, { name: "green shoes"} ] } I would like to query $search for "green" and return {name: "green shoes"} instead of returning the entire document.
  • Guest
    Apr 21, 2025
    Would this also fix sorting on embedded properties? I have used the filter in a projection to sort of make this work for hard filter values. But as pointed out this is not possible for things like autocomplete and text with fuzzy and such. On top of those issues I think this may solve sorting if I'm understanding correctly. Today if I sort on an embedded property it will sort all of the parents based on all of their embedded documents and not just the embedded documents that match. I don't think this sort can be moved as it's needed to create the next token for pagination. Would this feature fix this issue?
  • Guest
    Jul 10, 2024
    I have almost similar requirement. my document looks like this { "_id": "123", "name": "abc", "programmingLanguage": [ { "id": 1, "language": "Rust" }, { "id": 2, "language": "Python" } ] } Need to match only "Python" and return the result like this { "_id": "123", "name": "abc", "programmingLanguage": [ { "id": 2, "language": "Python" } ] }
  • Guest
    Jun 25, 2024
    To return only matching sub-documents within arrays, you can use an equivalent $filter expression in the $project stage of your pipeline. See Playground example: https://search-playground.mongodb.com/tools/code-playground/snapshots/667adca6fef99fff9972a251 This works well for $search expressions that have equivalent $filter expressions (e.g. equals, in, range), however, some query operators like autocomplete or text may need to be approximated with $regexMatch or similar. For more information, read the docs: https://www.mongodb.com/docs/atlas/atlas-search/embedded-document/#query-for-matching-embedded-documents-only
  • Guest
    Nov 21, 2022
    For those reading, there is an operator to search within the objects of arrays, however that will merely tell you that the string exists within the array of objects. It's called embeddedDocuments. To discover which object within the array matches a query, other methods like a materialized view may be needed.
  • Guest
    May 25, 2022
    I also need to be able to search within an array of documents and retrieve one document out of the array that matches my query params.