How to write a correct view and query for my data?

N1QL requires 4+. You should test your query using cbq shell. If that works, then you will be able to issue the query programmatically.