Querying “Not In” in Couchbase

狂风中的少年 提交于 2019-12-11 03:42:47

问题


I have a set of documents in Couchbase where each may have some users as editors - a set of UserID`s in array.

Id like to query all the docs where user with particular userid (username) is not in list of editors?

here's what my data looks like:

document1: {editors[1,2,3,4]}
document2: {editors[1,2,3,4,5]}

user{id:5}

Regarding the data above - how can I query all the docs of type document where user with id=5 is not listed in editors array - meaning only document1 should return?


回答1:


I can't think of a way to do this with Couchbase views, because, as I understand your description, this is a many-to-many relationship between documents and users, and you need to do a "not in" query. You can easily do with with N1QL, the new query language for Couchbase. It's currently in developer preview - whether this is a useful solution for you depends on how soon you need this code to go into production. If you need it right this minute then it's probably not the best solution. The other alternative is to use the ElasticSearch integration feature of Couchbase and do the query in ElasticSearch, then retrieve the relevant documents themselves from Couchbase. This also solves the task, with the benefit of being available for production right now.




回答2:


One additional alternative is to do this on the client side. you can retrieve all documents and their editors in a view and iterate over the list and throw away any doc where user_id 5 is present in the editor list through a loop. If you won't expect many rows will qualify for the "not in" clause, you won't be wasting too many cycles by doing this. This would be trivial with N1QL. you can set it up fast and try. developer preview is already available and we are currently working to release that in the next major release of the product. thanks -cihan




回答3:


Ok, i got one solution that suits my requirements. I have added a paging functionality to the page, which helped to not waste too much resources.

Basically i return 100 (depends on page size) docs per cycle of type "document". On each cycle i go through list of rows and select only those logged in user is not assigned to. In case i have 100 rows for a page i return then, otherwise i go on another cycle (page sized) and try to add missing rows to be returned for a view.

This is quite ugly, but i cant see any easy and light to go solution without queries.

Another thing was found - LINQ to Couchbase by Telerik



来源:https://stackoverflow.com/questions/27890297/querying-not-in-in-couchbase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!