How to attach score to each individual embedded document in Atlas search

Hey all!

I have a situation where we have a collection of clients, and each client has an array of pets. We need to search based on the client name and the pet name, and we want to locate not only the best client match, but also which pet matched the best.

Is there a way to score each individual embedded document? Or sort/filter by which embedded doc matches the best?

An example schema is this, imagine a collection of many clients like this.

  client_id: "123",
  name: "John Smith",
  pets: [
      pet_id: "abc",
      name: "Fluffy"
      pet_id: "xyz",
      name: "Spot"

Is there a way I can query on both the client name, and the pet name, resulting in the best matching client and pet pair? Im able to search for the doc, including embedded documents, but it just returns the whole document, with no indication as to which pet matches the best.

Thanks in advance!

Hi Jamis Hartley!

I’m almost 100% sure that it is not possible to get the score per embedded document. Atlas Search index points to the document as a whole and not a field or embedded document in it. The index knows that the pet with { name: Fluffy } is in the document of { client_id: "123" }, but where in the document, the index has no idea because it doesn’t store this type of information.

To do than you would need a $set stage that $filter the pets: array. Something like:

pet_name = "Spot" ;
pipeline = [
    /* your current pipeline stages */
    { "$set" : { "pets" :  { "$filter" : {
        "input" : "$pets" ,
        "cond" : { "$eq" : [ "$$" , pet_name ] }
    } } } }

Ah thats a shame! Thanks for your reply.

I was hoping that since it had some embedded documents scores in the scoreDetails metadata, there would be something.

Unfortunately this doesn’t quite work for my use case :confused:

The user may be submitting a query that doesn’t exactly match the pet name. We are using Mongo’s search to enable some fuzzy finding. Which works great, but means its trickier to filter down to the actual pet.

Is there a way to do some sort of fuzzy finding in the $set stage of the pipeline like you’ve described? Instead of the $eq condition.

You may take a look at

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.