如何使用Python从MongoDB读取数据?

f8rj6qna  于 2023-05-17  发布在  Go
关注(0)|答案(3)|浏览(255)

下面是JSON:

"work": {
    "secondary_sku": [],
    "internal_work": [ 
        {
            "name": "NCB",
            "LOB": "RUNME",
            "rm": "prabhu",
            "skills": [ 
                "python", 
                "javascript libraries", 
                "java applets", 
                "py", 
                "sqoop"
            ],
            "role": ObjectId("589c6da731beda27110af128"),
            "description": "Strong understanding of JavaScript, its quirks, and workarounds\n\nBasic understanding of web markup, including HTML5- and CSS3\n\nGood understanding of advanced JavaScript libraries and frameworks such as: AngularJS, KnockoutJS, Backbone.js, ReactJS, DurandalJS, Vue.js etc.\n\nGood understanding of asynchronous request handling- and partial page updates\n\nKnowledge of advanced JavaScript Concepts like Closures,- Promises- and Callbacks",
            "responsibility": "Strong understanding of JavaScript, its quirks, and workarounds\n\nBasic understanding of web markup, including HTML5- and CSS3\n\nGood understanding of advanced JavaScript libraries and frameworks su- ch as: AngularJS, KnockoutJS, Backbone.js, ReactJS, DurandalJS, Vue.js etc.\n\nGood understanding of asynchronous request handling- and partial page updates\n\nKnowledge of advanced JavaScript Concepts like Closures,- Promises- and Callbacks",
            "current": false,
            "start": ISODate("2017-03-30T00:00:00.000Z"),
            "end": ISODate("2017-03-31T00:00:00.000Z"),
            "billable": false,
            "client": "test"
        }
    ]
}

如何从mongodb中读取“工作”细节数据。我试着这样做:

work = [nref.get('internal_work', '') for nref in details.get('work', [{}])]

work = details["work"].get("internal_work","")

注意:details是循环变量的名称。

eqoofvh9

eqoofvh91#

我不太明白你的问题,因为你的Json样本格式不正确。提供有关唯一ID、集合名称和数据库名称的详细信息。无论如何,如果你使用Pyhon,我推荐使用PyMongo驱动程序。
使用PyMongo,搜索代码看起来像这样...

from pymongo import MongoClient
 
def main ():
     
     # Connection to the MongoDB Server
     mongoClient = MongoClient ('Your_IP: Your_Port')
     # Connection to the database
     db = mongoClient.Your_DataBase
     #Collection
     collection = db.Your_Colection
     
     details = collection.find ({"Work": "id_"})
             
if __name__ == "__main__":
         main ()

查询(find)将取决于Json的正确结构。
有关如何使用Pymongo的更多信息,请使用此link。问候

fnvucqvd

fnvucqvd2#

使用pymongo从mongodb读取数据

import pymongo
    import json
    client = pymongo.MongoClient("localhost", 27017)
    collection_USER = client[simple]
    Define_Collection_for_USER_FILE = collection_USER['USER']
    dict_for_user = {}
    for obj in Define_Collection_for_USER_FILE.find({}, {'_id': False}):
        dict_for_user.update(obj)

    d = dict_for_user
f3temu5u

f3temu5u3#

在2023年回答这个问题,所以我使用Python3从employees数据库中的details这样的记录集合中读取work中显示的具有上述结构的记录:

from pymongo import MongoClient

database_host_uri = "localhost:27017"   # or wherever the db host is
client = MongoClient(database_host_uri)
employees = client["employees"]
employee_details = employees.get_collection("details")

# assuming the "internal_work" list has only one element as above
for employee_record in employee_details.find():
    employee_work_info = employee_record["internal_work"][0]
    print(f"For {employee_work_info['name']} we have {employee_work_info['skills']}.")

如果你仍然需要在Python 2.7中找到答案,请注意PyMongo 3.12.0有deprecated support for 2.7,所以请确保你使用的版本不是比这个版本更新。

相关问题