Pymongo: how to add a new field to a document?

4,004 views
Skip to first unread message

Martin Wirz

unread,
Aug 26, 2011, 11:14:02 AM8/26/11
to mongodb-user
I have a collection of documents that look like:

{u'a': 492.0, u'c': 1, u'l': {u'lat': 116.331763, u'long': 39.937213},
u'u': 130, u't': 1230803098.0, u'_id':
ObjectId('4e5518be93585711374dfba5')}

Now, I would like to add a field v to that document using the update()
function in PyMongo:

coll.update({'_id': record_id}, {"$set": {'v': v, 'b': b, 'd':d}},
upsert=False)

This, however, returns the following error message: NameError: name
'v' is not defined.

I found this: http://stackoverflow.com/questions/4372797/how-do-i-update-a-mongo-document-after-inserting-it

However, I get the same NameError when trying to append 'v' to my
datapoint of the cursor element.

How can I do this?

Thanks a lot for your help.

Dan Crosta

unread,
Aug 26, 2011, 11:40:11 AM8/26/11
to mongod...@googlegroups.com
This python error means that you have no local variable named v.

You can't add a field with no value to a BSON document (nor to a Python dictionary, as we see in your error). If you meant to add it with a null BSON value, you can use:

    coll.update({'_id': record_id}, {'$set': {'v': None, 'b': None, 'd': None}})

Python's None is translated by pymongo to a BSON null. You can see a table of the other type mappings at http://api.mongodb.org/python/current/api/bson/son.html?highlight=null#bson.son.SON

- Dan

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Bernie Hackett

unread,
Aug 26, 2011, 6:17:19 PM8/26/11
to mongodb-user
What Dan said is exactly right but I just wanted to make it clear the
original error isn't actually with PyMongo.

> This python error means that you have no local variable named v.

Here's an example:

Python 2.7.1 (r271:86832, Jun 15 2011, 18:14:54)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> v
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'v' is not defined


On Aug 26, 8:40 am, Dan Crosta <dcro...@gmail.com> wrote:
> This python error means that you have no local variable named v.
>
> You can't add a field with no value to a BSON document (nor to a Python dictionary, as we see in your error). If you meant to add it with a null BSON value, you can use:
>
>     coll.update({'_id': record_id}, {'$set': {'v': None, 'b': None, 'd': None}})
>
> Python's None is translated by pymongo to a BSON null. You can see a table of the other type mappings athttp://api.mongodb.org/python/current/api/bson/son.html?highlight=nul...
>
> - Dan
>
> On Aug 26, 2011, at 11:14 AM, Martin Wirz wrote:
>
>
>
>
>
>
>
> > I have a collection of documents that look like:
>
> > {u'a': 492.0, u'c': 1, u'l': {u'lat': 116.331763, u'long': 39.937213},
> > u'u': 130, u't': 1230803098.0, u'_id':
> > ObjectId('4e5518be93585711374dfba5')}
>
> > Now, I would like to add a field v to that document using the update()
> > function in PyMongo:
>
> > coll.update({'_id': record_id}, {"$set": {'v': v, 'b': b, 'd':d}},
> > upsert=False)
>
> > This, however, returns the following error message: NameError: name
> > 'v' is not defined.
>
> > I found this:http://stackoverflow.com/questions/4372797/how-do-i-update-a-mongo-do...
Reply all
Reply to author
Forward
0 new messages