我在我的文档中有ctime
和mtime
(创建/修改时间)字段。CouchDB自动生成的字段
我该如何让couchDB为我处理这些?例如:
// Create a dog
curl -X POST http://localhost:5984/dogs -d '{"name": "Bill"}'
{"ok":true,"id":"75efaeb93aa2ed75ffa0abf9f5006d40","rev":"1-49ce25e3db701c8cb613c1fd18d99619"}
- >ctime
和mtime
应该是自动生成的
// Update a dog
curl -X PUT http://localhost:5984/dogs/75efaeb93aa2ed75ffa0abf9f5006d40?rev=1-49ce25e3db701c8cb613c1fd18d99619 -d '{"name": "BILL"}'
- >mtime
都应自动更新
我用validate_doc_update
来处理这个跳跃,类似于:
function (newDoc, oldDoc, userCtx) {
//
// sanity checks
//
if (oldDoc && newDoc.ctime) throw {"forbidden": "ctime cannot be changed!"}
...
//
// Auto-generate fields
//
var now = new Date().toISOString();
// mtime
newDoc.mtime = now;
// ctime
if (!oldDoc) newDoc.ctime = now;
}
,但没有运气:它的出现改变newDoc
没有效果(通过复制传递?)
谢谢
可能跟踪http://wiki.apache.org/couchdb/Document_Update_Handlers ...欢迎任何示例! :) – abernier
http://stackoverflow.com/questions/5810671/is-it-possible-to-add-fields-to-a-document-in-a-couchdb-validation-function – abernier
http://stackoverflow.com/questions/3009925/couchdb-automatic-timestamps – abernier