<--------------main function------------------->
From Pymongo import mongoclient
From Bson.objectid import Objectid
From Read_json Import Read
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) #传入json文件, query by keyword
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}}) #根据ObjectID修改track
Collection.update ((id), {' $push ': {' Nodeindex ': Want_no}}) #根据ObjectID添加nodeindex
Collection.update ((id), {' $set ': {' Nodeindex ': Want_no}}) #根据ObjectID修改nodeindex, remove superfluous []
Id_change = Collection.find_one (ID) #调出修改后的数据
Print ("Modified:")
Print (Id_change)
if __name__ = = ' __main__ ':
Mongoclient ()
<---------------Read file code----------------------->
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
Python reads all JSON data written to MongoDB (single)