V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ztoben
V2EX  ›  Python

mongo 如何对比一个文档的更改?

  •  
  •   ztoben · 2021-08-09 19:08:09 +08:00 · 1372 次点击
    这是一个创建于 962 天前的主题,其中的信息可能已经有所发展或是发生改变。
    各位大佬,mongo 如何对比一个文档改了哪些字端?更改了哪些值?
    比如 a = {'name': 'zz', 'age': 18, 'scope': [1,2,3]}
    更改为了 a = {'name': 'zzz', 'age': 28, 'scope': [4,5,6]}
    如何接口判断到哪个值变换了
    7 条回复    2021-08-13 19:57:21 +08:00
    SorcererXW
        1
    SorcererXW  
       2021-08-09 19:10:23 +08:00
    findOneAndUpdate({},{},{returnNewDocument:false}) 取出旧值再对比
    kyuuseiryuu
        2
    kyuuseiryuu  
       2021-08-09 21:38:17 +08:00 via iPhone
    function getChangedFields(oldObj, newObj): string[] {
    --const changedFields: string[] = [];
    --Object.keys(newObj).forEach(k => {
    ----if (newObj[k].toString() != oldObj[k].toString()) {
    ------changedFields.push(k);
    ----}
    --});
    --return changedFields;
    }
    ztoben
        3
    ztoben  
    OP
       2021-08-10 11:03:56 +08:00
    @SorcererXW 其实我的意思是有没有什么第三方的包或者通用的方法来判断他哪些字端的变更,比如 java 的 ObjectDiff
    crclz
        4
    crclz  
       2021-08-11 00:31:11 +08:00
    可以 Replace (全量更新)
    strict
        5
    strict  
       2021-08-13 15:11:57 +08:00
    db.coll.findAndModify()可以拿到原 json
    python 通过 jsondiff 去作比较
    ztoben
        6
    ztoben  
    OP
       2021-08-13 19:56:31 +08:00
    @strict jsondiff 我去看看这个包 感谢
    ztoben
        7
    ztoben  
    OP
       2021-08-13 19:57:21 +08:00
    @crclz 最主要不是更新 是记录他发生过的变化 从 a 变到 b 列表从空变到有值
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1227 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 18:07 · PVG 02:07 · LAX 11:07 · JFK 14:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.