简体   繁体   English

pymongo + update throw $ pull

[英]pymongo+update throw $pull

I have a mongo document: 我有一个mongo文件:

{ "_id" : 0, "name" : "Vasya", "fav" : [ { "type" : "t1", "weight" : 1.4163 }, { "type" : "t2", "weight" : 11.7772 }, { "type" : "t2", "weight" : 6.4615 }, { "type" : "homework", "score" : 35.8742 } ] }

For delete lowest element in array "fav", I use the following Python code: 对于删除数组“fav”中的最低元素,我使用以下Python代码:

db.people.update({"fav":{"type":"t2", "weight":lowest}}, {"$pull":{"fav"{"type":"t2", "weight":lowest}}})

where variable lowest is the lowest value between 6.4615 and 35.8742. 其中变量lowest值是6.4615和35.8742之间的最低值。

The problem is that this code does nothing. 问题是这段代码什么也没做。 There are no errors, and the values are not deleted from the array. 没有错误,并且不会从数组中删除值。 But if I write in the mongo shell the same code, the result is positive. 但是如果我在mongo shell中编写相同的代码,结果是正面的。

Unfortunately my experience in pymongo and in mongo is not so good. 不幸的是,我在pymongo和mongo的经历并不是那么好。 So if someone knows what the problem is, that would be great. 所以,如果有人知道问题是什么,那就太好了。

The syntax works fine for me in Mongo shell and with pymongo, so as suspected the issue is the precision of floating point numbers. 在Mongo shell和pymongo中,语法对我来说很好,所以怀疑问题是浮点数的精度。

I don't know how you are deriving/computing lowest but you may want to consider standardizing on maximum number of significant digits after the decimal point, or maybe even have a function that normalizes your floats to the same precision, both when you are originally saving documents and when you are later querying or updating them. 我不知道你如何推导/计算lowest但你可能想要考虑标准化小数点后的有效位数,或者甚至可能有一个函数将你的浮点数标准化到相同的精度,这两者都是你最初的保存文档以及稍后查询或更新文档。

Neither Mongo nor Python consider 6.676176060654615 to be equal to 6.67617606065 which explains why your update is having no effect. Mongo和Python都不认为6.676176060654615等于6.67617606065,这就解释了为什么你的更新没有效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM