简体   繁体   English

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

[英]How to Create MongoDB database table to following structure?

I have String Collection of data. 我有数据的字符串集合。 This is my data values. 这是我的数据值。

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'}

I split this data values using python as below and try to store in MongoDB database. 我按如下所示使用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)

I want to insert my data into MongoDB database as following: 我想将数据插入到MongoDB数据库中,如下所示:

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

When the second transaction happens, if coin id is equal to the previous record, I want to append new recode to the same "Transaction" array like below: 当第二笔交易发生时,如果硬币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"}] }

How should I implement the "Transaction" object to persist data using self.collection.insert(transaction) methods? 如何使用self.collection.insert(transaction)方法实现“事务”对象以持久化数据?

I tried the above code, but it gives me this error: 我尝试了上面的代码,但是它给了我这个错误:

exceptions.TypeError: 'dict' object is not callable. exceptions.TypeError:'dict'对象不可调用。

Otherwise it does not allow me to put "sender" :"value" , "receiver" :"value" , "date" :"value" this format within transaction array. 否则,不允许我将"sender" :"value" , "receiver" :"value" , "date" :"value"这种格式放入事务数组中。

To recap 回顾

How can I create a MongoDB collection like below, using PyMongo? 如何使用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"
                }
              ]
 }

I think it's a very basic task in MongoDB. 我认为这是MongoDB中非常基本的任务。 You can just create a method with a collection by referring to your DB. 您可以通过引用数据库来创建带有集合的方法。 Here's an example: 这是一个例子:

 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'

Further, you can refer the original code in git: 此外,您可以在git中引用原始代码:
https://github.com/umayanga123/scpp_python_node/blob/master/scpp_base/src/db/db_handler.py https://github.com/umayanga123/scpp_python_node/blob/master/scpp_base/src/db/db_handler.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM