I use Prisma ORM in a Next.js environment, and I want to search a database collection but restrict the search to the scope of data created by a specific user.
I used MongoDB Atlas Search with the $search operator, which includes a compound operator and a filter clause within it. The filter clause caused no data to be returned from the whole search.
Below is the whole pipeline setting.
{
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' },
},
},
],
}
I tried to post a topic on the MongoDB developer community, you can view more detail with this link, but I haven't received response yet. Does any body have a clue what I did wrong?
It's actually a Prisma bug. https://github.com/prisma/prisma/issues/15013#issuecomment-1381397966
Change the filter clause to the following worked.
filter: [
{
equals: {
value: { $oid: userId },
path: 'user_id',
},
},
],