简体   繁体   中英

How to insert a document with null value into mongodb using pymongo?

I have a.csv file with a few cells set to null and am trying to store this data in mongodb using pymongo. However I will be feeding this data to an API so I need the fields to be null instead of a string that says "null" . I tried setting the null cells with None -

data.append( {  'date' : row[0] if row[0] != 'null' else None} )

but this produces the following document-

{ "_id" : ObjectId("5f3713b36e752a743d5b293d") }

The field that I set to null is not present, which returns undefined when accessed.

Is there a way to set this to null?

Storing None puts nulls into the database.

>>> client.foo.foo.insert_one(dict(a=1, b=None))
<pymongo.results.InsertOneResult object at 0x7f6aaa348c00>
>>> client.foo.foo.find_one()
{'_id': ObjectId('5f372970ac9b70fe41e39672'), 'a': 1, 'b': None}

In shell:

MongoDB Enterprise ruby-driver-rs:PRIMARY> db.foo.find()
{ "_id" : ObjectId("5f372970ac9b70fe41e39672"), "a" : 1, "b" : null }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM