深入了解BsonDocuments

时间:2020-03-25 11:20:19

标签: c# mongodb

我有一个可以在代码中操作的bson文档。

由于我已经处理了许多此类文档(它们也很大),因此除了编写所有文档之外,我还需要使用diff(更改)批量更新它们。

我已经能够使用deep-diff包较早地完成此javascript,但是找不到类似的库,该库基本上可以区分两个不同的对象或bsdondocument的库,并提供与javascript对应部分类似的更改。

  const observableDiff = require('deep-diff').observableDiff;

  module.exports.getDiffObject = (original, document) => {
  const updated = {};
  const removed = {};

  observableDiff(original, document, function (change) {
    if (change.kind === 'D' || change.rhs === undefined) {
      // handle special condition here. we may have a value of 'undefined' set, handle it just as a deletion.
      const [path, value] = handleDelete(change);
      removed[path] = value;
    } else if (change.kind === 'N') {
      const [path, value] = handleAdd(change);
      updated[path] = value;
    } else if (change.kind === 'E') {
      const [path, value] = handleEdit(change);
      updated[path] = value;
    }
  });

  return [updated, removed];
};

const handleAdd = (change) => {
  return [
    change.path.map((entry) => { return entry; }).join('.'),
    change.rhs
  ];
};

const handleEdit = (change) => {
  return [
    change.path.map((entry) => { return entry; }).join('.'),
    change.rhs
  ];
};

const handleDelete = (change) => {
  return [
    change.path.map((entry) => { return entry; }).join('.'),
    1
  ];
};

有什么想法吗?

0 个答案:

没有答案
相关问题