如何按照以下结构创建 MongoDB 数据库 table?
How to Create MongoDB database table to following structure?
我有数据的字符串集合。这是我的数据值。
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"}] }
当第二笔交易发生时,如果 coin 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' object is not callable.
否则不允许我将"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"
}
]
}
我认为这是 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
我有数据的字符串集合。这是我的数据值。
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"}] }
当第二笔交易发生时,如果 coin 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' object is not callable.
否则不允许我将"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"
}
]
}
我认为这是 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