我有一个Dictionary列表,我想将列表的字典插入MongoDB。
示例:
hat = [{'index': 0, 'transactions': [], 'timestamp': 1550213571.912593, 'previous_hash': '0', 'nonce': 120, 'hash': '00e193195361aafda9d5c05666197dabb58be28972345ef9a7f254a1d4ef1bf1'}, {'_id': '00fd435c1b48d4e8f7a32f6307ecac29512fe9ef1b8c09fc12e673ad203d62e6', 'index': 1, 'transactions': [{'author': 'Zoyeb Mansuri', 'content': 'Zoyeb Mansuri', 'timestamp': 1550213596.8207283}], 'timestamp': 1550213598.8488019, 'previous_hash': '00e193195361aafda9d5c05666197dabb58be28972345ef9a7f254a1d4ef1bf1', 'nonce': 225, 'hash': '00fd435c1b48d4e8f7a32f6307ecac29512fe9ef1b8c09fc12e673ad203d62e6'}, {'_id': '00436460a0d4d3421e4bc5eec46885450f5ca8cacca9d23d480be19e6288129f', 'index': 2, 'transactions': [{'author': 'Akshay Godase', 'content': 'Akshay Godase', 'timestamp': 1550214711.1368275}], 'timestamp': 1550214713.4285166, 'previous_hash': '00fd435c1b48d4e8f7a32f6307ecac29512fe9ef1b8c09fc12e673ad203d62e6', 'nonce': 419, 'hash': '00436460a0d4d3421e4bc5eec46885450f5ca8cacca9d23d480be19e6288129f'}, {'_id': '00c555c788027d2d657607ef8822fc7aba5667bea08956dfbd75671e163f28a9', 'index': 3, 'transactions': [{'author': 'Raj Pandey', 'content': 'Raj Pandey', 'timestamp': 1550215521.8643162}], 'timestamp': 1550215524.8524983, 'previous_hash': '00436460a0d4d3421e4bc5eec46885450f5ca8cacca9d23d480be19e6288129f', 'nonce': 308, 'hash': '00c555c788027d2d657607ef8822fc7aba5667bea08956dfbd75671e163f28a9'}]
mat = {}
for data in hat:
mat.update(data)
collection.insert_one(mat)
Output : print(mat)
{'_id': '00c555c788027d2d657607ef8822fc7aba5667bea08956dfbd75671e163f28a9', 'index': 3, 'transactions': [{'author': 'Raj Pandey', 'content': 'Raj Pandey', 'timestamp': 1550215521.8643162}], 'timestamp': 1550215524.8524983, 'previous_hash': '00436460a0d4d3421e4bc5eec46885450f5ca8cacca9d23d480be19e6288129f', 'nonce': 308, 'hash': '00c555c788027d2d657607ef8822fc7aba5667bea08956dfbd75671e163f28a9'}
Want output : {{'index': 0, 'transactions': [], 'timestamp': 1550213571.912593, 'previous_hash': '0', 'nonce': 120, 'hash': '00e193195361aafda9d5c05666197dabb58be28972345ef9a7f254a1d4ef1bf1'}, {'_id': '00fd435c1b48d4e8f7a32f6307ecac29512fe9ef1b8c09fc12e673ad203d62e6', 'index': 1, 'transactions': [{'author': 'Zoyeb Mansuri', 'content': 'Zoyeb Mansuri', 'timestamp': 1550213596.8207283}], 'timestamp': 1550213598.8488019, 'previous_hash': '00e193195361aafda9d5c05666197dabb58be28972345ef9a7f254a1d4ef1bf1', 'nonce': 225, 'hash': '00fd435c1b48d4e8f7a32f6307ecac29512fe9ef1b8c09fc12e673ad203d62e6'}, {'_id': '00436460a0d4d3421e4bc5eec46885450f5ca8cacca9d23d480be19e6288129f', 'index': 2, 'transactions': [{'author': 'Akshay Godase', 'content': 'Akshay Godase', 'timestamp': 1550214711.1368275}], 'timestamp': 1550214713.4285166, 'previous_hash': '00fd435c1b48d4e8f7a32f6307ecac29512fe9ef1b8c09fc12e673ad203d62e6', 'nonce': 419, 'hash': '00436460a0d4d3421e4bc5eec46885450f5ca8cacca9d23d480be19e6288129f'}, {'_id': '00c555c788027d2d657607ef8822fc7aba5667bea08956dfbd75671e163f28a9', 'index': 3, 'transactions': [{'author': 'Raj Pandey', 'content': 'Raj Pandey', 'timestamp': 1550215521.8643162}], 'timestamp': 1550215524.8524983, 'previous_hash': '00436460a0d4d3421e4bc5eec46885450f5ca8cacca9d23d480be19e6288129f', 'nonce': 308, 'hash': '00c555c788027d2d657607ef8822fc7aba5667bea08956dfbd75671e163f28a9'}}
最后一个集合只是将其插入到Mongodb中。
当我这样做时,它只会在列表中插入最后一个数据。但是我想要列表中的所有数据。
答案 0 :(得分:1)
您正在尝试将字典插入MongoDB中吗?
所以而不是:
for data in hat:
mat.update(data)
collection.insert_one(mat)
使用
for data in hat:
mat.update(data)
collection.insert_one(mat)
您的数据库表现在将包含所有数据。
,而且,我认为您会误解字典的工作方式,或您的for循环在示例中的工作方式。如果没有指定键,则不能有包含字典的字典。 因此,如果您真的想要包含字典的字典;使用您的代码,它将类似于:
counter = 1
for data in hat:
mat[str(counter)] = data
counter += 1
使用计数器变量作为键。输出将如下所示:
{'1': {'nonce': 120, 'index': 0, 'hash': '00e193195361aafda9d5c05666197dabb58be28972345ef9a7f254a1d4ef1bf1', 'transactions': [], 'timestamp': 1550213571.912593, 'previous_hash': '0'}, '3': {'nonce': 419, 'index': 2, 'previous_hash': '00fd435c1b48d4e8f7a32f6307ecac29512fe9ef1b8c09fc12e673ad203d62e6', 'hash': '00436460a0d4d3421e4bc5eec46885450f5ca8cacca9d23d480be19e6288129f', 'transactions': [{'content': 'Akshay Godase', 'timestamp': 1550214711.1368275, 'author': 'Akshay Godase'}], 'timestamp': 1550214713.4285166, '_id': '00436460a0d4d3421e4bc5eec46885450f5ca8cacca9d23d480be19e6288129f'}, '2': {'nonce': 225, 'index': 1, 'previous_hash': '00e193195361aafda9d5c05666197dabb58be28972345ef9a7f254a1d4ef1bf1', 'hash': '00fd435c1b48d4e8f7a32f6307ecac29512fe9ef1b8c09fc12e673ad203d62e6', 'transactions': [{'content': 'Zoyeb Mansuri', 'timestamp': 1550213596.8207283, 'author': 'Zoyeb Mansuri'}], 'timestamp': 1550213598.8488019, '_id': '00fd435c1b48d4e8f7a32f6307ecac29512fe9ef1b8c09fc12e673ad203d62e6'}, '4': {'nonce': 308, 'index': 3, 'previous_hash': '00436460a0d4d3421e4bc5eec46885450f5ca8cacca9d23d480be19e6288129f', 'hash': '00c555c788027d2d657607ef8822fc7aba5667bea08956dfbd75671e163f28a9', 'transactions': [{'content': 'Raj Pandey', 'timestamp': 1550215521.8643162, 'author': 'Raj Pandey'}], 'timestamp': 1550215524.8524983, '_id': '00c555c788027d2d657607ef8822fc7aba5667bea08956dfbd75671e163f28a9'}}
答案 1 :(得分:0)
尝试一下
hat = [{dict1},{dict2},{dict3},{dict4}]
mat = {}
for data in hat:
for key in data.keys():
mat[key] = data[key]