通过pymongo更新MongoDB中的记录会导致大多数记录被删除 [英] Updating records in MongoDB through pymongo leads to deletion of most of them

查看:92
本文介绍了通过pymongo更新MongoDB中的记录会导致大多数记录被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的python代码中使用远程mongodb数据库.访问数据库的代码和数据库本身位于两台不同的计算机上.我正在使用的pymongo模块版本是1.9+. 该脚本包含以下代码:

Im working with a remote mongodb database in my python code.The code accessing the database and the database itself are on two different machines. The pymongo module version im using is 1.9+. The script consists of the following code:

 for s in coll.find({ "somefield.a_date" : { "$exists":False },
                               "somefield.b_date" : { "$exists":False }}):
            original = s['details']['c_date']

            utc = from_tz.localize(original).astimezone(pytz.utc)

            s['details']['c_date'] = utc

            if str(type(s['somefield'])) != "<type 'dict'>":
                     s['somefield'] = {}

            s['somefield']['b_date'] = datetime.utcnow()

            coll.update({ '_id' : s['_id'] }, s );

运行此代码后,发生了一件奇怪的事情.最初在集合中有数百万条记录,运行该脚本后,仅剩余29%的记录,其余的被自动删除. PyMongo驱动程序1.9+版是否存在任何已知问题? 可能还有其他原因导致这种情况,以及以什么方式我可以找出确切发生了什么情况?

After running this code, a strange thing happened. There were millions of records in the collection initially and after running the script,just 29% of the total records remained, the rest were automatically deleted. Is there any known issue with PyMongo driver version 1.9+ ? What could have been other reasons for this and any ways i can find out what exactly happened ?

推荐答案

造成这种情况的其他原因可能是什么,我可以通过什么方式找出确切发生了什么情况?

What could have been other reasons for this and any ways i can find out what exactly happened ?

首先要检查的是是否有任何例外情况" ?

coll.update()中,您没有设置safe变量.如果update上存在异常,则不会引发该异常.

In coll.update(), you are not setting the safe variable. If there is an exception on the update, it will not be thrown.

在您的代码中,您不会捕获异常(建议),并且更新不会检查异常,因此您无法知道正在发生什么.

In your code you not do not catch exceptions (which is suggested) and your update does not check for exceptions, so you have no way of knowing what's going on.

要检查的第二件事是您如何计数" ?

update命令可以清空"数据,但是不能删除数据(或更改_id).

The update command can "blank out" data, but it cannot delete data (or change an _id).

您有原始数据的副本吗?您可以在这10个或100个中的少数几个上运行代码,看看发生了什么吗?

Do you have a copy of the original data? Can you run your code on a small number of those 10 or 100 and see what's happening?

您所描述的内容与任何MongoDB驱动程序都不正常.我们肯定需要更多数据来解决此问题.

What you describe is not normal with any of the MongoDB drivers. We definitely need more data to resolve this issue.

这篇关于通过pymongo更新MongoDB中的记录会导致大多数记录被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆