carte blanche
carte blanche

Reputation: 11476

AttributeError: 'list' object has no attribute 'update'

I am trying to update the value of project['submission']['status'] to queued for each matching version as below but running into a compilation error,any pointers on how to fix it is really appreicated?

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"
                            }
                        })

Document:

{
    "_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" : [],
}

Error:-

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

UPDATE:-

'''
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'
                     }
                  }
            )

Upvotes: 0

Views: 21762

Answers (4)

carte blanche
carte blanche

Reputation: 11476

Following works,it should be collection.update instead of 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'
                     }
                  }
                )

Upvotes: 2

Daniel da Rocha
Daniel da Rocha

Reputation: 947

Try:

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"})

This will add the property "sanity":"queued" to the project in the loop. Is that what you want?

Upvotes: 1

shubham verma
shubham verma

Reputation: 53

You have to update your document.

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

This should work.

You can also iterate all the projects assign the status and at the end of iteration update the whole document at once, this will also work.

Upvotes: 0

TerryA
TerryA

Reputation: 60004

The value to the key "projects" is a list, not a dictionary. You need to append your new list:

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

And change the syntax as such.

Upvotes: 0

Related Questions