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.
# 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:
{ "_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" : "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?
I tried the above code, but it gives me this error:
exceptions.TypeError: 'dict' object is not callable.
Otherwise it does not allow me to put "sender" :"value" , "receiver" :"value" , "date" :"value"
this format within transaction array.
How can I create a MongoDB collection like below, using PyMongo?
{
_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. 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:
https://github.com/umayanga123/scpp_python_node/blob/master/scpp_base/src/db/db_handler.py