PyMongo upsert에서 "upsert는 bool의 인스턴스 여야합니다."오류 발생
Python에서 MongoDB에 대한 업데이트를 실행하고 있습니다. 이 줄이 있습니다.
self.word_counts[source].update({'date':posttime},{"$inc" : words},{'upsert':True})
그러나 다음 오류가 발생합니다.
raise TypeError("upsert must be an instance of bool")
그러나 True
나에게는 bool의 인스턴스처럼 보입니다!
이 업데이트를 올바르게 작성하려면 어떻게해야합니까?
PyMongo의 세 번째 인수가 update()
됩니다 upsert
및 부울이 아닌 사전을 통과해야합니다. 코드를 다음과 같이 변경하십시오.
self.word_counts[source].update({'date':posttime}, {"$inc" : words}, True)
또는 upsert=True
키워드 인수로 전달 :
self.word_counts[source].update({'date':posttime}, {"$inc" : words}, upsert=True)
귀하의 실수는 가능성에 대한 책을 읽은 의해 발생 된 update()
에서 MongoDB를 워드 프로세서 . 의 자바 스크립트 버전 update
과 같은 선택적 매개 변수가 포함 된 세 번째 인자로 오브젝트를 upsert
와 multi
. 그러나 Python은 키워드 인수를 함수에 전달할 수 있기 때문에 (위치 인수 만있는 JavaScript와 달리) 이것은 불필요하며 PyMongo는 대신 이러한 옵션을 선택적 함수 매개 변수로 사용합니다.
http://api.mongodb.org/python/2.3/api/pymongo/collection.html#pymongo.collection.Collection.update 에 따르면 실제로 upsert를 True가 아닌 키워드로 전달해야합니다.
self.word_counts[source].update({'date':posttime},{"$inc" : words},**{'upsert':True})
또는
self.word_counts[source].update({'date':posttime},{"$inc" : words},upsert=True)
당신이 이제까지와 같은 다른 kwargs로 통과 할 것처럼 단지 사실 전달보다 더 나은 접근 방식 safe
또는 multi
인수의 순서가 유지되지 않으면 코드가 휴식 할 수는.
upsert는 다음과 같이 위치 매개 변수로 전달되어야합니다.
self.word_counts[source].update(
{'date':posttime},
{"$inc" : words},
True)
또는 키워드 인수로
self.word_counts[source].update(
{'date':posttime},
{"$inc" : words},
upsert=True)
'programing' 카테고리의 다른 글
.trigger () 대 .click ()의 jQuery 장점 / 차이점 (0) | 2020.11.12 |
---|---|
R에서 Rprof를 효율적으로 사용하는 방법은 무엇입니까? (0) | 2020.11.12 |
Scheme과 Common Lisp의 실제 차이점은 무엇입니까? (0) | 2020.11.11 |
Objective C의 사유 재산 (0) | 2020.11.11 |
gson에서 MalformedJsonException 발생 (0) | 2020.11.11 |