CouchDb: How to delete documents older > 6 month?
I have the problem that I like to "automagically" delete documents in my couch, which are at least 6 month old. My CouchDb instance runs on开发者_Python百科 a linux server, is there any way to achieve this quite simple (like writing a simple 2-line shell script) ?
You can write an update function in couchdb that deletes a doc on certain criteria ( you can use params while calling the function) : http://wiki.apache.org/couchdb/Document_Update_Handlers#Creating_an_Update_Handler
(look at "in-place" and imagine setting "_delete:true").
something like
"deletefunc":
...
if(doc.created_at<req.query.mindate) {
doc._deleted:true;
return [doc, "deleted"]
}
and calling ...db/_design/updatefuncdesigndoc/_update/deletefunc/dok_id_x?mindate=20110816
The only work is: calling each doc in a database explicit with this function (calling _all_docs or _changes first)
As long as I know, CouchDB stores each database (with all documents) in a single file (). So you will be not able to find specific doc by its name or added-datetime.
UPDATE:
I think the only way would be to add a "_doc_created" (or "_doc_established") field to each document, with a timestamp (similarity to SQLs NOW()
). Then create a view which shows only documents IDs an values of "_doc_created" fields:
e.G.
function(doc) {
emit(doc._doc_created, doc._id);
//or just emit(doc._doc_created) 'couse views alsways return docIDs
}
and then write a script (e.G. a shell-script) which gets all these IDs and DATEs (via curl), filters it and then (again via curl) DELETEs all the docs from database which _doc_crated
datetime is older than 6 month from now
精彩评论