Reputation: 9014
I have the following document structure in a couchdb database:
{
'type': 'Message',
'subject': 'Cheap V1@gr@',
'body': 'not spam',
'metadata': {
'participation': {
'read': [1,2,3]
}
}
}
The read
array is a list of user ids who have read a message. I can easily create a view to allow me to filter the list of messages by the messages a user has read, like so:
function(doc) {
doc['metadata']['participation']['read'].forEach(function(user_id) {
emit(user_id, doc._id);
});
}
and then to execute the query for user_id = 1
:
curl -X GET \
http://localhost:5984/couchrest/_design/Message/_view/included_in_read?key=1
My question is: how would I go about writing a view/query to return documents for which the user's id is NOT included in the read
array, i.e. "unread" messages.
Upvotes: 2
Views: 616
Reputation: 73722
You need to use a technique called the Thai massage.
The link above is my description of it in an email, and I described it in an answer to a similar question on Stack Overflow: Find CouchDB docs missing an arbitrary field
I think the variation you would need to do is emit a 2-array for the key, [user_id, doc_id]
. When you query for "hits" for user 1234
, use ?startkey=[1234,null]&endkey=[1234,{}]
to show only the hits for that user.
Upvotes: 1
Reputation: 103
You can use the following Map function,
function(doc) {
if(!doc.metadata.participation.read)
emit(doc.user_id, doc);
}
and then to execute the query for user_id = 1:
curl -X GET \
http://localhost:5984/couchrest/_design/Message/_view/included_in_read?key=1
Upvotes: 0