Find difference between 2 documents on mongoDB from the mongo shell

后端 未结 1 1123
面向向阳花
面向向阳花 2021-02-15 12:38

I\'m using mongodb 2.4.4 and I want to compare 2 documents, then print their differences only using the mongo shell. Is there a way to compare them? Something like:



        
相关标签:
1条回答
  • 2021-02-15 13:14

    Just declare native javascript function that can compare two objects in a way you need, then write a code like this:

    obj1 = db.test.findOne({"_id" : ObjectId("5176f80981f1e2878e840888")})
    obj2 = db.test.findOne({"_id" : ObjectId("5176f82081f1e2878e840889")})
    difference(obj1, obj2)
    

    Some native javascript difference functions can be found here or here

    P.S. You can also load some third party js libs from shell like this:

    load("D:\difference.js")
    

    Hope this help.

    0 讨论(0)
提交回复
热议问题