MongoDB update dictionary value in a list of dictionaries

591 views
Skip to first unread message

wookie

unread,
Nov 25, 2014, 9:10:59 AM11/25/14
to mongod...@googlegroups.com
I have a script that checks the date of each `dict` value and if it's less than today's date it changes that date value in the `dict`. The `dict` is stored in a `list` of 'dicts'.

I'm trying to update db value for that dict value like this:

        for user in db.user.find({'Expenses.ExpirationAlert':{'$lte':todayDate}}):

            exp
= user['Expenses']
                             
            gen
= (each for each in exp if each['ExpirationAlert'] <= todayDate)
               
           
for each in gen:
               
print each['title']
                newData
={}
                newData
={
               
'Expenses.ExpirationAlert' : '99999'
               
}
           
                db
.user.update({'Expenses.id':"01"},{"$set":newData})



Where `user['Expenses']` is the list of dicts.
 
`id` is a key in each dict.

This however doesn't reset the `ExpirationAlert` value to 99999.

How could I do this for each dict?

Asya Kamsky

unread,
Nov 26, 2014, 9:38:35 AM11/26/14
to mongodb-user
If all of those dictionaries are embedded in a single document then
you cannot do what you describe - see SERVER-1243.

If you frequently need to make updates like this, then this is the
wrong schema for your use case and you should store the dictionaries
as documents themselves, not embedded as a list into another
document/dictionary.

Asya
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user"
> group.
>
> For other MongoDB technical support options, see:
> http://www.mongodb.org/about/support/.
> ---
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mongodb-user...@googlegroups.com.
> To post to this group, send email to mongod...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mongodb-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mongodb-user/98472ae1-cec7-4a07-bc41-a0f4284520e7%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages