如何将MongoDB数据库表创建为以下结构?

时间:2016-11-16 09:09:12

标签: python mongodb python-2.7 pymongo non-relational-database

我有String Collection数据。这是我的数据值。

INFO:utils.senz_parser:{'#time': '1479283854.49', '#RECIVER': 'kk', '#f': 'td', '#M_S_ID': 'M_1', '#S_PARA': '3', '#FORMAT_DATE': '2016-11-1613:41:18', '#NO_COIN': '1', '#S_ID': '2', '#pubkey': 'LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlHZk1BMEdDU3FHU0liM0RRRUJBUVVBQTRHTkFEQ0JpUUtCZ1FDdlZ6QU5YdUUvM0Y5a2VRc2JHbVRWNDY0WApjWWQ5dnl1OHM4MlZRcXh0NFJBQitHREVGSHRHRjlQOEtzVFNvRjJYMWVEeVpNOS9NTENLVFB2dGN5bkluOVExCnl6YVZwTm9xQWcwaXI5enB2MSszZzRWazZBUGR6QUdmZ1NCSmtnYjJHUzNpa21KdjVEYmhjY0d0dmQ5akx0NHcKQjNJYWtRUnBSSkRnRUVaWE9RSURBUUFCCi0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQ==', '#msg': 'ALREADY_REGISTERED', '#COIN': 'e5df02bac93f541f8a1db177f52f006c1afbeb0a'}

我使用python分割这些数据值,如下所示,并尝试存储在MongoDB数据库中。

 # added new method
def addCoinWiseTransaction(self, quarry):
    self.collection = self.db.transaction_detail
    coinValexists = self.collection.find({"_id": quarry["#COIN"]}).count()
    print('coin exists : ', coinValexists)
    if (coinValexists > 0):
        print('coin hash exists')
        //IF coin is exist add new record to Transaction array

    else:
        print('new coin mined')
        //create  transaction json object but pass error
        transaction = {"_id": quarry["#COIN"]
                       , "S_ID": int(quarry["#S_ID"]),
                       "S_PARA": quarry("#S_PARA"), "FORMAT_DATE": quarry("#FORMAT_DATE"),"TRANSACTION": [quarry["#M_S_ID"], quarry["#RECIVER"],int(quarry["#NO_COIN"]),datetime.datetime.utcnow()]}


        self.collection.insert(transaction)

我想将我的数据插入到MongoDB数据库中,如下所示:

{ "_id" : "value" , "Service_ID" :"value" ,"parameters" :"value " ,"Formate Date" :"value" ,"Transaction" :[ {"sender" :"value" , "receiver" :"value" , "date" :"value"}] }

当第二个交易发生时,如果硬币ID等于前一个记录,我想将新的recode附加到同一个"交易"数组如下:

{ "_id" : "value" , "Service_ID" :"value" ,"parameters" :"value " ,"Formate Date" :"value" ,"Transaction" :[ {"sender" :"value" , "receiver" :"value" , "date" :"value"},{"Sender" :"A1" ,"receiver" :"a2" , "date" ,"value"}] }

我应该如何实施"交易"使用self.collection.insert(transaction)方法保留数据的对象?

我尝试了上面的代码,但它给了我这个错误:

  

exceptions.TypeError:' dict'对象不可调用。

否则它不允许我在事务数组中放置"sender" :"value" , "receiver" :"value" , "date" :"value"这种格式。

回顾一下

如何使用PyMongo创建如下所示的MongoDB集合?

 {
   _id: "joe",
   name: "Joe Bookreader",
   addresses: [
                {
                  street: "123 Fake Street",
                  city: "Faketon",
                  state: "MA",
                  zip: "12345"
                },
                {
                  street: "1 Some Other Street",
                  city: "Boston",
                  state: "MA",
                  zip: "12345"
                }
              ]
 }

1 个答案:

答案 0 :(得分:1)

我认为这是MongoDB中的一项非常基本的任务。您可以通过引用数据库来创建带有集合的方法。这是一个例子:

 def addCoinWiseTransaction(self, quarry):
    self.collection = self.db.transaction_detail
    coinValexists = self.collection.find({"_id": quarry["#COIN"]}).count()
    print('coin exists : ', coinValexists)
    if (coinValexists > 0):
        print('coin hash exists')
        newTransaction = {"$push": {"TRANSACTION": {"SENDER": quarry["#SENDER"],
                                                    "RECIVER": quarry["#RECIVER"],
                                                    "T_NO_COIN": int(quarry["#T_NO_COIN"]),
                                                    "DATE": datetime.datetime.utcnow()
                                                    }}}
        self.collection.update({"_id": quarry["#COIN"]}, newTransaction)
    else:
        print('new coin mined')
        root = {"_id": quarry["#COIN"]
            , "S_ID": int(quarry["#S_ID"]), "S_PARA": quarry["#S_PARA"], "FORMAT_DATE": quarry["#FORMAT_DATE"],
                "NO_COIN": int(quarry["#NO_COIN"]),
                "TRANSACTION": [{"MINER": quarry["#M_S_ID"],
                                 "RECIVER": quarry["#RECIVER"],
                                 "T_NO_COIN": int(quarry["#NO_COIN"]),
                                 "DATE": datetime.datetime.utcnow()
                                 }
                                ]
                }
        self.collection.insert(root)

    return 'DONE'

此外,您可以在git中引用原始代码:
https://github.com/umayanga123/scpp_python_node/blob/master/scpp_base/src/db/db_handler.py