AttributeError:'list'对象没有属性'update'

时间:2018-03-23 05:34:17

标签: python mongodb

我正在尝试为每个匹配的版本更新project['submission']['status']queued的值,如下所示,但遇到编译错误,有关如何修复它的任何指针真的很有用吗?

from pymongo import MongoClient
from bson.objectid import ObjectId
import os,pymongo
dbuser = os.environ.get('muser', '')
dbpass = os.environ.get('mpwd', '')
uri = 'mongodb://{dbuser}:{dbpass}@machineip/data'.format(**locals())
client = MongoClient(uri)
db = client.data
collection = db['test']
print db.version

cursor = collection.find({})
cursor = collection.find({})
for document in cursor:
      if document['version'] == '9.130.39.0.32.6.1':
        for project in  document['projects']:
            print project
            print project['name']
            print project['_id']
            id = project['_id']
            print project['submission']['status']
            if project['submission']['status'] != 'queued':
                print "Inside Update.."               
                #project['submission']['status'] = 'queued'
                collection_projects = document['projects']
                #print collection_projects
                for project in collection_projects:
                    project.update(
                        { "_id": ObjectId(id) },
                        {
                            "$set": {
                                "sanity": "queued"
                            }
                        })

文件:

{
    "_id" : ObjectId("5a95a1c32a2e2e0025e6d6e2"),
    "status" : "Submitting",
    "sanity" : "none",
    "version" : "9.130.39.0.32.6.1",
    "requestTime" : ISODate("2018-02-27T18:21:55.764Z"),
    "projects" : [ 
        {
            "name" : "BCMFurm_4364_B2_ekans",
            "_id" : ObjectId("5a95a1c32a2e2e0025e6d6eb"),
            "submission" : {
                "status" : "passed", --> this status should change to "queued"
                "system" : "machine.com"
            }
        }, 
        {
            "name" : "BCMFurm_4364_B2_sid",
            "_id" : ObjectId("5a95a1c32a2e2e0025e6d6ea"),
            "submission" : {
                "status" : "passed",--> this status should change to "queued"
                "system" : "machine.com"
            }
        }, 
        {
            "name" : "BCMFurm_4364_Notes",
            "_id" : ObjectId("5a95a1c32a2e2e0025e6d6e3"),
            "submission" : {
                "status" : "passed",--> this status should change to "queued"
                "system" : "machine.com"
            }
        }
    ],
    "Notes" : [],
}

错误: -

  "sanity": "queued"
TypeError: update expected at most 1 arguments, got 2

更新: -

'''
for document in cursor:
      if document['version'] == '9.130.39.0.32.6.1':
        for project in  document['projects']:
            print project
            project_id = project['_id']
            if project['submission']['status'] != 'queued':
                project.update(
                    { "_id" : ObjectId(project_id)},
                        { "$set":
                            {
                                "submission.status": "queued"
                            }
                        }
                )
'''
for document in cursor:
    docId = document['_id']
    print "docId"
    print docId
    if document['version'] == '9.130.39.0.32.6.1':
        for project in  document['projects']:
            print "project"
            print project
            projectId = project['_id']
            print "projectId"
            print projectId
            document.update({ "_id": ObjectId(docId), "projects._id": ObjectId(projectId) },
                  { 
                    '$set': {
                      'projects.$.submission.status': 'queued'
                     }
                  }
            )

4 个答案:

答案 0 :(得分:1)

以下作品应该是db.update而不是for document in cursor: docId = document['_id'] print "docId" print docId if document['version'] == '9.130.39.0.32.6.1': for project in document['projects']: print "project" print project projectId = project['_id'] print "projectId" print projectId collection.update({ "_id": ObjectId(docId), "projects._id": ObjectId(projectId) }, { '$set': { 'projects.$.submission.status': 'queued' } } )

{{1}}

答案 1 :(得分:0)

"projects"的值是列表,而不是字典。您需要append新列表:

collection_projects.append([
                        { "_id": ObjectId(id) },
                        {
                            "$set": {
                                "sanity": 'queued'
                            }])

并改变语法。

答案 2 :(得分:0)

尝试:

for project in  document['projects']:
        print project
        print project['name']
        print project['_id']
        id = project['_id']
        print project['submission']['status']
        if project['submission']['status'] != 'queued':
                print "Inside Update.."               
                project.update({"sanity": "queued"})

这会将属性"sanity":"queued"添加到循环中的项目中。这就是你想要的吗?

答案 3 :(得分:0)

您必须更新文档。

  var docId = document._id;
  var projectId = project._id;
  document.update({ "_id": ObjectId(docId), "projects._id": ObjectId(projectId) },
                  { 
                    '$set': {
                      'projects.$.submission.status': 'queued'
                     }
                  }
  );

这应该有用。

您还可以迭代所有项目分配状态,并在迭代结束时立即更新整个文档,这也可以。