根据rfc6902,RethinkDB对部分更新json文档有用吗?

Eri*_*rik 3 json mongodb node.js rethinkdb json-patch

请分享您对部分更新JSON文档的经验.现在我将我的JSON文档存储在MongoDB中,如下所示:

{
  id: ObjectId(507f191e810c19729de860ea),
  title: 'Sterling Archer',    
  comments: [
    {text: 'Comment text', tags: ['tag1', 'tag2', 'tag3']},
    {text: 'Comment test', tags: ['tag2', 'tag5']}
  ]  
}
Run Code Online (Sandbox Code Playgroud)

我需要经常使用rfc6902规范更新我的文档.现在,我没有优化的方式,看起来如下(我在这个例子中使用nodejs/express/mongoose和fast-json-patch模块):

var jsonpatch = require('fast-json-patch');

app.patch('/document/:id', function (req, res, next) {
   var document_id = req.params.id;

   // req.body.patch: { "op": "add", "path": "/comments/2", "value":  {text: 'Comment test3', tags: ['tag4']}" }
   var patches = req.body.patch; 

   // find document
   Document.findById(document_id, function (err, document) {
       // Applying patches
       jsonpatch.apply(document, patches);

       // Update whole document in MongoDB
       Document.update({_id: document_id}, document, function (err) {
           return res.status(200).send(); 
       });
   });
});
Run Code Online (Sandbox Code Playgroud)

由于MongoDB中的两个查询并替换整个文档,因此这不是优化补丁文档的方法.所以我正在寻找优化的方法,并希望尝试RethinkDB来完成这项任务.你能帮助我通过使用RethinkDB的单一查询来检查原子文档更新的可能性吗?或者我应该寻找另一种解决问题的方法吗?

请分享您对部分更新JSON文档的经验.

neu*_*ino 6

您只需要在RethinkDB中查询一个.假设您要更新id为1{foo:1,bar:2} 的文档,并增加字段"count",您可以

r.table("data").get(1).update(function(doc) {
    return doc.merge({foo: 1, bar:2, count: doc("count").add(1) })
})
Run Code Online (Sandbox Code Playgroud)

虽然此更新需要唯一的查询,但整个文档将更新.如果您有大文档,则可以将它们拆分为多个表,然后再执行连接以检索数据.您可能有兴趣阅读有关数据建模的这篇文章:http://www.rethinkdb.com/docs/data-modeling/