mongodb中的update()返回什么

2024-06-25 23:20:26 发布

您现在位置:Python中文网/ 问答频道 /正文

在mongodb中为api编写python脚本时。。在

我们有。。在

new_posts = [{ 'name': 'A', 'age': 17, 'marks': 97, 'school': 'School1' },
             { 'name': 'B', 'age': 18, 'marks': 95, 'school': 'School2' },
             { 'name': 'C', 'age': 19, 'marks': 97, 'school': 'School2' }]

db.posts.insert( new_posts )

我们创建索引如下。。在

^{pr2}$

现在我们要做两个手术。。在

db.posts.update({ 'name':'A', 'age': 17, 'school': 'School3' },
                { 'name':'D', 'age':  17, 'marks': 70, 'school': 'School1' },
                   upsert=True )

db.posts.update({ 'name':'A', 'age': 17, 'school': 'School1' },  
                { 'name':'A', 'age': 17, 'marks': 60, 'school': 'School1' },
                   upsert=True )

update()在这里返回什么?如何确定文档是插入到数据库中还是现有文档被更新?在

我们能做些像。。在

post1 = db.posts.update({ 'name':'A', 'age': 17, 'school': 'School3' },
                        { 'name':'D', 'age':  17, 'marks': 70, 'school': 'School1' },
                        upsert=True )

post2 = db.posts.update({ 'name':'A', 'age': 17, 'school': 'School1' },
                        { 'name':'A', 'age': 17, 'marks': 60, 'school': 'School1' },
                           upsert=True )

print post1

print post2

Tags: name文档truenewdbageupdatepost1
1条回答
网友
1楼 · 发布于 2024-06-25 23:20:26

正如updatedocs所说,该方法返回:

A document (dict) describing the effect of the update or None if write acknowledgement is disabled.

只需尝试一下,然后print返回值,看看有什么可用的。你会看到这样的东西:

{u'syncMillis': 0, u'ok': 1.0, u'err': None, u'writtenTo': None,
 u'connectionId': 190, u'n': 1, u'updatedExisting': True}

updatedExisting字段就是您要查找的。在

相关问题 更多 >