2017-01-13 2 views
0

Я только начинаю изучать Mongodb и pymongo. Я намеревался расширить новый список до значения списка документа, я пытался находку(), но ничего не выйдет .. После моя структура данных:Как получить значение из документа Mongodb?

{ "_id" : ObjectId("58784108b0925e52ec4c478b"), "ip" : "61.228.93.0", "history" : [ { "aging_type" : "static", "from" : "dragonresearchgroup.org", "threat" : "Scanners", "updated_time" : "2016-05-11T20:29:51" } ], "updated_time" : "2017-01-13T10:52:56" } 
{ "_id" : ObjectId("58784108b0925e52ec4c478c"), "ip" : "61.231.1.0", "history" : [ { "aging_type" : "static", "from" : "dragonresearchgroup.org", "threat" : "Scanners", "updated_time" : "2016-05-15T15:54:17" } ], "updated_time" : "2017-01-13T10:52:56" } 
{ "_id" : ObjectId("58784108b0925e52ec4c478d"), "ip" : "220.135.220.0", "history" : [ { "aging_type" : "static", "from" : "dragonresearchgroup.org", "threat" : "Scanners", "updated_time" : "2016-05-11T08:42:41" } ], "updated_time" : "2017-01-13T10:52:56" } 
{ "_id" : ObjectId("58784108b0925e52ec4c478e"), "ip" : "111.248.98.0", "history" : [ { "aging_type" : "static", "from" : "dragonresearchgroup.org", "threat" : "Scanners", "updated_time" : "2016-05-15T06:21:08" } ], "updated_time" : "2017-01-13T10:52:56" } 
{ "_id" : ObjectId("58784108b0925e52ec4c478f"), "ip" : "111.243.254.0", "history" : [ { "aging_type" : "static", "from" : "dragonresearchgroup.org", "threat" : "Scanners", "updated_time" : "2016-05-11T00:54:30" } ], "updated_time" : "2017-01-13T10:52:56" } 
{ "_id" : ObjectId("58784108b0925e52ec4c4790"), "ip" : "111.248.102.0", "history" : [ { "aging_type" : "static", "from" : "dragonresearchgroup.org", "threat" : "Scanners", "updated_time" : "2016-05-14T04:10:31" } ], "updated_time" : "2017-01-13T10:52:56" } 
{ "_id" : ObjectId("58784108b0925e52ec4c4791"), "ip" : "36.229.236.0", "history" : [ { "aging_type" : "static", "from" : "dragonresearchgroup.org", "threat" : "Scanners", "updated_time" : "2016-05-13T22:51:34" } ], "updated_time" : "2017-01-13T10:52:56" } 

Предположим, что я хочу, чтобы получить «историю» значение чье значение «ip» равно «61.228.93.0», а затем добавьте [{'aging_type':static,'updated_time':2017-1-13T10:55:22}] к этому значению списка, затем обновите это значение «история» с расширенным списком, что я должен делать? Я попытался db.collection.find({"ip":'ip'})['history'] и db.collection.find({"ip":'ip'},{'history':1}), но это кажется не поможет .. я был бы признателен, если кто-то рода может помочь мне с этой проблемой

+1

попробовать это 'db.collection.find ({IP:«61.228.93.0»})', чтобы получить значения которых ф is '61.228.93.0' –

ответ

1

Чтобы получить значение истории для документа по IP:

db.collection.find_one({'ip': '61.228.93.0'})['history'] 

чтобы добавить в массив истории:

result = db.collection.update_one(
    {'ip': '61.228.93.0'}, 
    {'$push': {'history': {'aging_type': 'static', 
          'updated_time': '2017-1-13T10:55:22'}}}) 

print(result.raw_result) 

печатается:

{'updatedExisting': True, u'nModified': 1, u'ok': 1.0, u'n': 1} 

Чтобы добавить в массив истории и получить обновленный документ в одной команде:

from pymongo import ReturnDocument 
result = db.collection.find_one_and_update(
    {'ip': '61.228.93.0'}, 
    {'$push': {'history': {'aging_type': 'static', 
          'updated_time': '2017-1-13T10:55:22'}}}, 
    return_document=ReturnDocument.AFTER) 

print(result)