在mongo shell中找到mongoDB上2个文档之间的区别 [英] Find difference between 2 documents on mongoDB from the mongo shell
问题描述
我正在使用mongodb 2.4.4,我想比较2个文档,然后仅使用mongo shell打印它们之间的差异.有没有办法比较它们?像这样:
db.collection.compare({first_doc: objectID("blablalba"), sec_doc: objectID("blalba2")})
输出将类似于
{diff1:{latitude:{first_doc:10.000, sec_doc:20.000}},diff2:{}}
其中纬度是具有差异的字段的名称.
输出不必完全像这样,但可以提供相同的功能. 谢谢
只需声明本机javascript函数即可按照您需要的方式比较两个对象,然后编写如下代码:
obj1 = db.test.findOne({"_id" : ObjectId("5176f80981f1e2878e840888")})
obj2 = db.test.findOne({"_id" : ObjectId("5176f82081f1e2878e840889")})
difference(obj1, obj2)
可以找到此处 >
P.S.您还可以像这样从shell加载一些第三方js库:
load("D:\difference.js")
希望获得帮助.
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:
db.collection.compare({first_doc: objectID("blablalba"), sec_doc: objectID("blalba2")})
and the output would be something like
{diff1:{latitude:{first_doc:10.000, sec_doc:20.000}},diff2:{}}
where latitude is the name of the field that had a difference.
The output doesn't need to be exactly like that, but give the same functionality. Thanks
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.
这篇关于在mongo shell中找到mongoDB上2个文档之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!