user3162206
user3162206

Reputation:

How to update multiple values in Mongodb using pymongo?

In my code when a process starts the following data are stored in my core_pid table.

db.core_pid.insert({"blog_id": blog["_id"], "proid": str(p.pid), "starttime": datetime.now(pytz.timezone('EST')), "status": "Running", "route":routing})

After the process is completed i updated the value of status using the below code.

db.core_pid.update({"_id": current_id},
                       {"$set": {"status": "Completed"}}) 

Now i want to include a new field endtime in database when process is completed ({"endtime": datetime.now(pytz.timezone('EST'))}).How can i alter the above update code to include this endtime field too.

Upvotes: 5

Views: 15998

Answers (2)

Neil Lunn
Neil Lunn

Reputation: 151112

Simply add the other field as an argument to $set:

b.core_pid.update({"_id": current_id},
    {"$set": { 
        "status": "Completed",
        "endtime": datetime.now(pytz.timezone('EST')) 
    }})

And that will just update both of those fields in the document without touching others.

Upvotes: 0

seikichi
seikichi

Reputation: 1281

Simply, include the "endtime" field in "$set" value

db.core_pid.update({"_id": current_id},
                   {"$set": {
                       "status": "Completed",
                       "endtime": datetime.now(pytz.timezone('EST'))}}) 

Upvotes: 11

Related Questions