连接mongoDB根据ObjectID写入json数据(初步)

Posted Moucong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了连接mongoDB根据ObjectID写入json数据(初步)相关的知识,希望对你有一定的参考价值。

from pymongo import MongoClient
from bson.objectid import ObjectId
import json


def read(data, find):
# 设置以utf-8解码模式读取文件,encoding参数必须设置,否则默认以gbk模式读取文件,当文件中包含中文时,会报错
f = open(data, encoding=‘utf-8‘)
setting = json.load(f)

# 注意多重结构的读取语法
# family = setting[‘BaseSettings‘][‘font‘]
style = setting[find]


# print(family)
return style



def mongoclient():
client = MongoClient(‘127.0.0.1‘, 27017)
db = client.test
collection = db.test
# collection.insert({})
# for i in collection.find({"name":"乐清市东岸北段"}):
# print(i)
data = (‘coastlines.real.json‘)
find_Da = (‘track‘)
find_Id = (‘coastlineid‘)
find_Node = (‘nodes_index‘)

want_Data = read(data, find_Da)
want_Id = read(data, find_Id)
want_No = read(data, find_Node)
id = {‘_id‘: ObjectId(want_Id)}
id_print = collection.find_one(id)
print(id_print)
collection.update((id), {‘$set‘: {‘track‘: want_Data}})
# collection.insert((id), {‘$set‘: {‘track‘: want_Data}})
# collection.insert((id),{‘$set‘:{"nodeindex" : want_No}})


id_change = collection.find_one(id)
print("修改后:")
print(id_change)




if __name__ ==‘__main__‘:
mongoclient()






















































以上是关于连接mongoDB根据ObjectID写入json数据(初步)的主要内容,如果未能解决你的问题,请参考以下文章

mongodb:从 json 查询中的 ObjectID 中提取时间戳

mongodb:从 json 查询中的 ObjectID 中提取时间戳

MongoDB ObjectID 的 JSON 序列化(ex-scope 查询)

在 C# 中从序列化为 JSON 的 mongodb 文档中删除 ObjectId

如何在 HTTP 请求 JSON 正文中传递 mongodb ObjectId()?

MongoDB PHP:如何使用 JSON 提要获取 ObjectId? (它是空白的)