简体   繁体   中英

Can couchDB automatically update a specific doc when a new doc has been added?

I'm very new to CouchDB. I understand I can create reduce functions that get called from stuff outside CouchDB. But what if I want CouchDB to do something automatically when a new doc is added to the DB? For example, say I have this db:

{_id:1, type:stats, meanscore:3}
{_id:2, type:event, date:02/01/2016, score:4}
{_id:3, type:event, date:02/02/2016, score:2}

Now say I send a new "event" doc to the db. Can couchDB automatically calculate the mean of the "score" values and update doc id 1 with the new mean value?

No, CouchDB can't do that out of the box. Typically an application layer sitting over CouchDB would handle that kind of arbitrary code requirements.

However, your specific example is exactly the kind of thing view reduce functions are for. If this isn't just to illustrate your question, but is actually what you want to do then you should use a reduce function.

For me your question is more related to the case for a change-feed ( http://docs.couchdb.org/en/1.6.1/api/database/changes.html ) in the application Layer.

In general, you hold a request to a specific database change sequence number open (longpoll). On document-insertion to that database you get a callback for every time a new document was added immediately. In this callback function you can easily change another specific document.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM