from pymongo import MongoClient
client = MongoClient()
db = client.test
coll = db.dataset
from datetime import datetime
result = db.restaurants.insert_one(
{
"address": {
"street": "2 Avenue",
"zipcode": "10075",
"building": "1480",
"coord": [-73.9557413,40.7720266]
},
"borough": "Manhattan",
"cuisine": "Italian",
"grades": [
{
"date": datetime.strptime("2014-10-01", "%Y-%m-%d"),
"grade": "A",
"score": 11
},
{
"date": datetime.strptime("2014-01-16", "%Y-%m-%d"),
"grade": "B",
"scroe": 17
}
],
"name": "Vella",
"restaurant_id": "41704620"
}
)
result.inserted_id
我有上面提到的python代码。在这里,我使用 insert_one() 向 MongoDB 插入了一个文档。我的问题是如何将该数据存储在变量中并在 insert_one() 方法中使用该变量?例如,db.restaurants.insert_one(somthng) 其中 somthng 是将存储文档的变量。
你是这个意思吗?
something = {
"address": {
"street": "2 Avenue",
"zipcode": "10075",
"building": "1480",
"coord": [-73.9557413,40.7720266]
},
"borough": "Manhattan",
"cuisine": "Italian",
"grades": [
{
"date": datetime.strptime("2014-10-01", "%Y-%m-%d"),
"grade": "A",
"score": 11
},
{
"date": datetime.strptime("2014-01-16", "%Y-%m-%d"),
"grade": "B",
"scroe": 17
}
],
"name": "Vella",
"restaurant_id": "41704620"
}
result = db.restaurants.insert_one(something)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句