是什么在MongoDB中收益的update() [英] What does update() in mongodb returns

查看:124
本文介绍了是什么在MongoDB中收益的update()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然在MongoDB中。

编写Python脚本的API

我们有..

  new_posts = [{'名':'A','年龄':17'标志':97,学校:School1},
             {'名':'B','年龄':18'标志':95,学校:School2},
             {'名':'C','年龄':19'标志':97,学校:School2'}]db.posts.insert(new_posts)

我们创建索引,如下所示。

  db.posts.create_index([('名',1),('学校',1)],独特= TRUE)

现在我们执行两种操作。

  db.posts.update({'名':'A','年龄':17,学校:School3},
                {'名':'D','年龄':17'标志':70,学校:School1},
                   UPSERT = TRUE)db.posts.update({'名':'A','年龄':17,学校:School1},
                {'名':'A','年龄':17'标志':60,学校:School1},
                   UPSERT = TRUE)

什么是更新()返回在这里?我们怎样才能找出天气将文档插入到数据库或现有的文档进行更新?

我们可以这样做。

  POST1 = db.posts.update({'名':'A','年龄':17,学校:School3},
                        {'名':'D','年龄':17'标志':70,学校:School1},
                        UPSERT = TRUE)POST2 = db.posts.update({'名':'A','年龄':17,学校:School1},
                        {'名':'A','年龄':17'标志':60,学校:School1},
                           UPSERT = TRUE)打印POST1打印POST2


解决方案

随着<一个href=\"http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.update\"相对=nofollow>文档为更新说,该方法返回:


  

描述的更新效力的文件(字典)或如果写确认是禁用的。


只是试一试,打印的返回值来看看有什么可用。你会看到类似这样的:

  {u'syncMillis':0,u'ok':1.0,u'err':无,u'writtenTo':无,
 u'connectionId':190,u'n:1,u'updatedExisting:真正}

updatedExisting 字段是你在找什么。

While writing python script for the api in mongodb..

We have..

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 )

We create indexes as follows..

db.posts.create_index([('name',1),('school',1)],unique=True)

Now we perform two operations..

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 )

What does the update() returns here? How can we find out weather the document is inserted into the db or existing document is updated?

Can we do something like..

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

解决方案

As the docs for update say, the method returns:

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

Just try it and print the return value to see what's available. You'll see something like:

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

The updatedExisting field is what you're looking for.

这篇关于是什么在MongoDB中收益的update()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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