From Pymongo import mongoclient
From Bson.objectid import Objectid
Import JSON
def read (data, find):
# set to read the file in Utf-8 decoding mode, encoding parameter must be set, otherwise the default in GBK mode to read the file, when the file contains Chinese, will error
f = open (data, encoding= ' Utf-8 ')
setting = Json.load (f)
# Note The read syntax for multiple structures
# 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": "Leqing East Coast North Section"}):
# 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 ("Modified:")
Print (Id_change)
if __name__ = = ' __main__ ':
Mongoclient ()
Connect MongoDB to write JSON data based on Objectid (preliminary)