No search results for $facet inside $search

We have an issue running facet queries for Atlas Search. We have a collection containing documents like these;

{ _id: ObjectId("650ae6b0f2e2c55ce7c3c3c9"), contentTypeId: ObjectId("650ae6aef2e2c55ce7c3c21a"), contentTypeIdString: "650ae6aef2e2c55ce7c3c21a" ... }

We use an Atlas $search / $searchMeta aggregation pipeline to return the amount of results in the ‘contentTypeIdString’ facet. But we want to retrict the documents to only those documents matching a specific contentTypeId. If we run this Atlas search it works fine and the query returns 309 results:

{
  facet: {
    operator: {
      exists: {
        path: "contentTypeIdString",
      },
    },
    facets: {
      contentTypeFacet: {
        type: "string",
        path: "contentTypeIdString",
        numBuckets: 100,
      },
    },
  },
}

However, if we exchange the “contentTypeIdString” property with “contentTypeId”, no results at all are returned:

{
  facet: {
    operator: {
      exists: {
        path: "contentTypeId",
      },
    },
    facets: {
      contentTypeFacet: {
        type: "string",
        path: "contentTypeIdString",
        numBuckets: 100,
      },
    },
  },
}

The only difference is that contentTypeId is of type ObjectId where contentTypeIdString is of type String. Also this query does not return any results, even though I’m sure this document exists:

{
  facet: {
    operator: {
      equals: {
        path: "contentTypeIdString",
		value: "650ae6aef2e2c55ce7c3c21a"
      },
    },
    facets: {
      contentTypeFacet: {
        type: "string",
        path: "contentTypeIdString",
        numBuckets: 100,
      },
    },
  },
}

Why do the last two queries not return any results?

Hi @Engatta_Team and welcome to MongoDB community forums!!

Firstly, $exists in MongoDB is the aggregation stage which filters out the results based on the availability of the filed name.
This does not filter the documents based of the field values. You can make use of $match stage if you wish to do so.

From the information posted above, there could be two different ways to understand about facets.

1. $facet as aggregation stage. In this case, the $facet categories the data in multiple dimensions to perform the query on which means it performs multiple aggregations within the same stage and process the output. If you are using $exits inside the $facet stage, it would filter out the results to be processed. You can visit the example for further information.

2. $facet inside $search as an operator: This operator would groups results by values or ranges in the specified faceted fields and returns the count for each of those groups. This operator could be applied to String, numeric and Date fields which have been defined as StringFacet, NumericFacet and DateFacets respectively. You can visit the examples and the index definitions for usage of facets in the String Facets documentations.

In your case, it sounds like you want to filter documents by the ObjectId value before faceting. To do this, you can use a $match stage first to filter the relevant documents by the ObjectId, and then apply the $facet stage to aggregate over those results

However, if this is not what you are looking for, could you provide me the sample documents and the desired output from the documents. Also, please share the index query being created.

Warm regards
Aasawari

Hi Assawari,

Thank you for your answer! Indeed, I want to filter documents by the ObjectId value before faceting. I was confusing the filtering of results versus filtering of documents. So with your answer its clear to me what I should change. Thanks again!

Regards, Ivo

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