如何使用python从mongodb读取数据?

时间:2017-10-25 13:20:47

标签: mongodb python-2.7

这是json:

onBackPressed()

如何从mongodb中读取“工作”详细信息数据。 我试过这样:

"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 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",
                "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"
            }
        ]

work = details [“work”]。get(“internal_work”,“”)

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

2 个答案:

答案 0 :(得分:1)

我不太了解您的问题,因为您采样的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 ()

查询(查找)将取决于Json的正确结构。

有关如何使用Pymongo的更多信息,请使用此link。 的问候,

答案 1 :(得分:0)

使用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