Python read all JSON data written to MongoDB (single)

Source: Internet
Author: User
Tags mongoclient

<--------------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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.