【问题标题】:How can I do partial update or other methods to update complex documents in PouchDb?如何进行部分更新或其他方法来更新 PouchDb 中的复杂文档?
【发布时间】:2017-09-21 17:41:42
【问题描述】:

我有一个像这样的复杂文档:

    {
   "_id": "07394B10-DEB7-E703-BB97-37B694FA0877",
   "_rev": "2-9a2c5809802024a8d35cc9fbba9ea885",
   "name": "Ivrea",
   "number": "1",
   "owners": [
       {
           "name": "Ale",
           "address": "Via Ale 2",
           "gender": "Uomo",
           "type": "Assente",
           "date": "2014-08-10",
           "notes": [
               {
                   "text": "Foo",
                   "date": "2014-08-10"
               }
           ]
       }
   ]
}

我怎样才能部分更新它?前任。只有owners.name 或owners.notes.date ? 如果我将使用链接文档方法进行“加入”,我该如何处理此示例拆分所有者和注释? 感谢您的回答!

【问题讨论】:

    标签: javascript json join pouchdb nosql


    【解决方案1】:

    连接似乎确实是您最好的选择,因为在 PouchDB 中无法仅更新文档的“部分”——您必须更新整个内容。

    这样的事情怎么样?假设您有车主和汽车。

    var owner = {
      _id: 'owner_1', // could be 'owner_' + Math.random() or whatever
      name: 'Cookie Monster',
      type: 'owner'
    };
    
    var car = {
      _id: 'car_1', // ditto
      name: 'Mach 5',
      type: 'car',
      ownerId: 'owner_1' // Cookie Monster drives the Mach 5 :)
    };
    
    var db = new PouchDB('mydb');
    
    db.bulkDocs([owner, car]).then(function () {
      return db.put({
        _id: '_design/owners_and_cars',
        views: {
          owners_and_cars: {
            map: function (doc) {
              if (doc.type === 'car') {
                emit(doc._id, {_id: doc.ownerId});
              }
            }.toString()
          }
        }
      }).catch(function (err) {
        if (err.status !== 409) {
          // 409 means already exists
          throw err;
        }
      });
    }).then(function () {
      return db.query('owners_and_cars', {include_docs: true});
    }).then(function (res) {
      console.log(JSON.stringify(res));
    }).catch(function (err) { 
      /* ... */
    });
    

    打印出来:

    {
      "total_rows": 1,
      "offset": 0,
      "rows": [
        {
          "id": "car_1",
          "key": "car_1",
          "value": {
            "_id": "owner_1"
          },
          "doc": {
            "name": "Cookie Monster",
            "type": "owner",
            "_id": "owner_1",
            "_rev": "1-f702c1d279add84b30c6464070d8a985"
          }
        }
      ]
    }
    

    但是,如果您有大量数据,这可能会很慢,因为二级索引很慢。在这种情况下,您可以明确地执行allDocs 查询:

    db.allDocs({startkey: 'car_', endkey: 'car_\uffff'}); // finds all cars
    db.allDocs({startkey: 'owner_', endkey: 'owner_\uffff'}); // finds all owners
    

    然后您将手动进行连接。

    【讨论】:

    • 感谢您的回复,但是对于具有大量数据的应用程序,此解决方案并不精简... :-( 我希望 PouchDB 将来会更新像 MongoDb 这样的文档功能类似于 $set 在单个字段中... ;-)
    • 你很幸运,因为这个插件昨天才出现:github.com/redgeoff/delta-pouch
    • 作者忘记建了。我提交了an issue
    • 我还想看看 factoryng 一个用于 delta-pouch 的 angularjs 适配器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 2017-06-03
    • 2016-08-18
    • 1970-01-01
    • 2022-10-07
    • 2014-06-29
    相关资源
    最近更新 更多