python Mongodb 操做

  1. 鏈接數據庫python

    1. 導入操做mongo包 數據庫

      •  import pymongo學習

    2. 鏈接數據庫spa

      1. client = pymongo.MongoClient('localhost',27017)

        Client 是產生一個鏈接數據庫的對象code

      2. ceshi = client['db_name']

        dbname: 數據庫的名字對象

                  若是忘了 終端 show dbs -------找到想要操做的數據庫it

        ceshi : 本身取有意義的名字io

      3. item_info = ceshi['Collection_name']

        Collection_name: 指想要操做數據庫中的表table

                  一樣忘記了,進入終端 use dbs 進入上面上述數據庫,show tablesclass

        item_info : 本身取有意義的名字


         


2.操做數據庫 -------查詢

  • 查詢全部Document  (我喜歡叫Record) 爲了更好的學習Mongodb 仍是叫 Document

for document in item_info.find():
    print(documnet)
  • 查詢 N條Document 

for document in item_info.find().limit(N):
    #注意 N 爲本身選擇 例如 10條,20條以內
    print(document)
  • 查詢 Document 中某個field(字段)

for document in item_info.find().limit(N):
    print(document['field_name'])

       field_name 改爲你 Document中 的字段

  • 查詢 全部Document 中對應 的field 和value

for document in item_info.find({'field':value}):
    print(document)
  • 查詢 條件(Condition)字段

{ <field1>: { <operator1>: <value1> } }
相關文章
相關標籤/搜索