Database

[{title:"Post1",content:"This is test post 1.",tags:["post","12345"]},{title:"Post2",content:"This is test post 2.",tags:["post","789"]},{title:"Post3",content:"This is test post 3.",tags:["post","123"]}]

Query

db.collection.aggregate([/** Find all tags that begin with the given query*/{$match:{"tags":{$regex:"^123"}}},{$addFields:{/** Add a field 'true' where one of the string values is an exact match of the query*/"exactMatch":{$ne:[{$filter:{input:"$tags",cond:{$eq:["$$this","123"/** query*/]}}},[]]},/** Add a field 'true' where the first 3 characters of one of the string values match the query*/"startsWith":{$ne:[{$filter:{input:"$tags",cond:{$eq:[{$substr:["$$this",0,3]},"123"/** query*/]}}},[]]}}},/** Prioritize exact matches first*/{$sort:{"exactMatch":-1,"startsWith":-1,}},/** Remove the added fields from results*/{$project:{"exactMatch":0,"startsWith":0}}])

Result