Is there a n1ql query to sort array of objects based on most matched and unmatched fields

Ex: I have an array of skills
Doc 1:
{
skills : [
{
“name” : "C Programming,
“experience” : “2years”
},
{
“name” : “NodeJS”,
“experience” : “2years”
}
]

Doc 2:
{
skills : [
{
“name” : “AWS”,
“experience” : “2years”
},
{
“name” : “NodeJS”,
“experience” : “2years”
}
]

Doc 3:
{
skills : [
{
“name” : "C Programming,
“experience” : “2years”
},
{
“name” : “C++”,
“experience” : “2years”
}
]

If the input is [“C Programming”, “C++”], the result should be in the order of Doc1, Doc3, and Doc2
Is this possible using n1ql?

SELECT d.*
FROM default AS d
WHERE .....
ORDER BY ARRAY_COUNT(ARRAY 1 FOR v IN d.skills WHEN v.name IN ["C Programming","C++"] END);

Its working @vsr1 …Can you please let me know what ‘ARRAY 1’ does? Even ‘ARRAY 0’ is working

In Your case any value will work (it builds array of 1’s , based on WHEN condition). I can used binding variable v, but in this case not needed and will reduce memory. You can put expression in projection and see what it produces.

SELECT d.*, 
          (SELECT RAW COUNT(1) FROM d.skills AS sk WHERE sk.name  IN ["C Programming","C++"])[0] matches;
FROM default AS d
WHERE .....
ORDER BY  matches DESC;