Reputation: 13
My users have one string field, let's call it "goodsId" and they also have one string array field, let's call it "viewedGoods". What I hope to do is write a query that will get the next non-viewed goods item, meaning our user will be looking at other users' goods and the query will check to see if their goodsId is already in our user's viewedGoods array. If the goodsId is not in our user's viewedGoods then return.
Ex1:
Our User {
-goodsId: "3wdj9"
-viewedGoods: ["djen9", "2wk01"]
}
Queried User 1 {
-goodsId: "edij2"
-viewedGoods: ["2l1k4"]
}
Ex2:
Our User {
-goodsId: "3wdj9"
-viewedGoods: ["djen9", "2wk01"]
}
Queried User 1 {
-goodsId: "djen9"
-viewedGoods: ["2l1k4"]
}
Queried User 2 {
-goodsId: "sds33"
-viewedGoods: ["2l1k4"]
}
Ex1: Our User will view Queried User 1's goods next
Ex2: Our User has already viewed Queried User 1's goods, so it views the next non-viewed goods (Queried User 2)
Upvotes: 1
Views: 35
Reputation: 317322
No, this is not possible with a single query with your current database structure. The way that Firestore indexes fields, you can only query for the presence of items in an array, not the absence of items. If you want to find some value in a list or map, that value has to be present.
What you would have to do here is store a list of both viewed and not-viewed items, then query the list that makes sense for what you're trying to do. Obviously, for big lists, this will not scale, and you will have to look into another solution.
Upvotes: 1