Filter by ObjectId with equals operator in $search pipeline stage does not work

Firstly, I have a pipeline that works well:

{
	pipeline: [
		{
			$search: {
				index: 'full-text-index',
				compound: {
					// filter: [
					// 	{
					// 		equals: {
					// 			value: new ObjectId(userId),
					// 			path: 'user_id',
					// 		},
					// 	},
					// ],
					should: [
						{
							autocomplete: {
								path: 'sentence',
								query: term,
								tokenOrder: 'any',
								fuzzy: {
									maxEdits: 2,
									prefixLength: 1,
									maxExpansions: 256,
								},
							},
						},
						{
							text: {
								query: term,
								path: 'note',
							},
						},
						{
							text: {
								query: term,
								path: 'translation',
							},
						},
					],
					minimumShouldMatch: 1,
				},
				sort: { score: { $meta: 'searchScore' } },
			},
		},

		{ $limit: ENTRIES_PER_PAGE },
		{
			$addFields: {
				id: { $toString: '$_id' },
				sentencePlusPhoneticSymbols: '$sentence_plus_phonetic_symbols',
			},
		},
		{
			$project: {
				_id: 0,
				id: 1,
				sentencePlusPhoneticSymbols: 1,
				translation: 1,
				note: 1,
				score: { $meta: 'searchScore' },
			},
		},
	],
}

except that the commented-out filter clause will break things up, causing no data to be returned from this pipeline.

I use bson package for the ObjectId constructor, which should be legit.

Below is the index settings:

{
  "mappings": {
    "dynamic": true,
    "fields": {
      "note": {
        "multi": {
          "note_chinese": {
            "analyzer": "lucene.chinese",
            "searchAnalyzer": "lucene.chinese",
            "type": "string"
          }
        },
        "type": "string"
      },
      "sentence": [
        {
          "type": "string"
        },
        {
          "type": "autocomplete"
        }
      ],
      "translation": {
        "analyzer": "lucene.chinese",
        "searchAnalyzer": "lucene.chinese",
        "type": "string"
      }
    }
  }
}

I attached a document in the target collection below. What I did wrong? Please help.

Hi @Adam_Hao Can you try removing the new keyword? Depending on the driver or interface you are using to query, there may be a different requirement for representing ObjectIds. Here’s an example that I recreated in a Search Playground snapshot using a subset of your query.

the commented-out filter clause will break things up, causing no data to be returned from this pipeline.

Are you getting an error or no results?

Thanks for your reply.
But I’ve figured it out. I posted the solution one hour ago, and it’s pending approval. In short, it is Prisma rather than MongoDB to blame, a bug that fortunately has a workaround.

It actually has nothing to do with MongoDB, it’s a Prisma bug.

Solution: Aggregate Raw not working when trying to `$match` on `_id` (needs to be an `ObjectId` but it's not a valid input) · Issue #15013 · prisma/prisma · GitHub

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