python 讀取單所有json資料寫入mongodb(單個)

來源:互聯網
上載者:User

標籤:去除   pytho   cti   font   mongod   pen   設定   date   讀取檔案   

<--------------主函數------------------->

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":"樂清市東岸北段"}):
# print(i)
data = (‘coastlines.real.json‘)
find_Da = (‘track‘)
find_Id = (‘coastlineid‘)
find_Node = (‘nodes_index‘)

want_Data = read(data, find_Da) #傳入json檔案,根據關鍵字查詢
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 ,去除多餘的[]


id_change = collection.find_one(id) #調出修改後的資料
print("修改後:")
print(id_change)




if __name__ ==‘__main__‘:
mongoclient()

<---------------讀取檔案代碼----------------------->
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

python 讀取單所有json資料寫入mongodb(單個)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.