2017-01-13 6 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})하지만 누군가가 어떤 IP에 의한 문서의 이력 값 검색하려면이 문제

+1

이'db.collection.find 시도 ({ "61.228.93.0"IP}) '61.228.93.0' –

답변

1

좀 도와 수 있다면 나는 감사하게 될 거라고 .. 을 더 도움이 보인다했습니다

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} 

은 역사의 배열에 추가하고 하나의 명령에 업데이트 된 문서를 효율적으로 활용하려면 다음 그 IP 값을 얻기 위해`:

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)