'Collection' 对象不可调用。如果你打算在 'Collection' 对象上调用 'save' 方法,它会失败,因为不存在这样的方法
'Collection' object is not callable. If you meant to call the 'save' method on a 'Collection' object it is failing because no such method exists
我正在使用 tornado 构建一个简单的网站 mongoDB。我使用 python 3.7 和最新版本的 pymongo 来控制 MongoDB 中的数据,但是当我使用 save
方法编辑 MongoDB 中的数据时,如下代码,出现以下错误:
TypeError: 'Collection' object is not callable. If you meant to call the 'save' method on a 'Collection' object it is failing because no such method exists.
def post(self, isbn=None):
import time
book_fields = ['isbn', 'title', 'subtitle', 'image', 'author', 'date_released', 'description']
burtbook = self.application.db.BurtBook
book = dict()
if isbn:
book = burtbook.find_one({"isbn":isbn})
for key in book_fields:
book[key] = self.get_argument(key, None)
if isbn:
burtbook.save(book)
else:
book['add_released'] = int(time.time())
burtbook.insert_one(book)
self.redirect("/recommended/")
请帮我修复这个错误。
嗯,我不知道为什么,但我认为自 2020 年底以来 save
或其 deprecated
存在错误?
救星方法将是:update_one($old_document, $keys_to_update)
我在 this reference 上看到您可以就地更新 document
并保持 ObjectID
不变(您将拥有原始文档 ID)
你的代码会变成(注释会说明一切)
def post(self, isbn=None):
import time
book_fields = ['isbn', 'title', 'subtitle', 'image', 'author', 'date_released', 'description']
burtbook = self.application.db.BurtBook
book = dict()
if isbn:
book = burtbook.find_one({"isbn": isbn})
# set is a specifier to mongo to update the values;
# you just need to pass which value to modify on which key
# example
# keys_to_update = {"$set": { "bookName": "newNameSetByYou"}}
# but the bookName key name remains the same
keys_to_update = {"$set": {}}
for key in book_fields:
value = self.get_argument(key, None)
book[key] = value
if isbn:
# here if isbn we create that dictionary to tell
# mongo which value to modify on which key
keys_to_update["$set"].update({
key: value
})
# simple and old pythonic dict update
if isbn:
# here we update the document book with new values
# from the above generated dict
burtbook.update_one(book, keys_to_update)
# just like the save()
# save is deprecated boys, cant use it anymore ...
else:
book['add_released'] = int(time.time())
burtbook.insert_one(book)
self.redirect("/recommended/")
okey,为我辩护,我无法测试你的代码,但我已经在我的项目中测试了相同的技术并且它知道它对我有用。
告诉我它是否有效。步伐
编辑
另一个有用的reference
你也可以试试这个代码(在这里你可以用你想要的任何东西替换整个文档):
todos_collection = get_collection(todos_collection_name)
requested_todo = todos_collection.find_one({
"_id": ObjectId(oid)
})
todos_collection.replace_one(requested_todo, {"something": "else"})
# this will replace the entire document `request_todo` with `{"something": "else"}`
# but it will keep the original `ObjectID`
# and thats is very useful
我正在使用 tornado 构建一个简单的网站 mongoDB。我使用 python 3.7 和最新版本的 pymongo 来控制 MongoDB 中的数据,但是当我使用 save
方法编辑 MongoDB 中的数据时,如下代码,出现以下错误:
TypeError: 'Collection' object is not callable. If you meant to call the 'save' method on a 'Collection' object it is failing because no such method exists.
def post(self, isbn=None):
import time
book_fields = ['isbn', 'title', 'subtitle', 'image', 'author', 'date_released', 'description']
burtbook = self.application.db.BurtBook
book = dict()
if isbn:
book = burtbook.find_one({"isbn":isbn})
for key in book_fields:
book[key] = self.get_argument(key, None)
if isbn:
burtbook.save(book)
else:
book['add_released'] = int(time.time())
burtbook.insert_one(book)
self.redirect("/recommended/")
请帮我修复这个错误。
嗯,我不知道为什么,但我认为自 2020 年底以来 save
或其 deprecated
存在错误?
救星方法将是:update_one($old_document, $keys_to_update)
我在 this reference 上看到您可以就地更新 document
并保持 ObjectID
不变(您将拥有原始文档 ID)
你的代码会变成(注释会说明一切)
def post(self, isbn=None):
import time
book_fields = ['isbn', 'title', 'subtitle', 'image', 'author', 'date_released', 'description']
burtbook = self.application.db.BurtBook
book = dict()
if isbn:
book = burtbook.find_one({"isbn": isbn})
# set is a specifier to mongo to update the values;
# you just need to pass which value to modify on which key
# example
# keys_to_update = {"$set": { "bookName": "newNameSetByYou"}}
# but the bookName key name remains the same
keys_to_update = {"$set": {}}
for key in book_fields:
value = self.get_argument(key, None)
book[key] = value
if isbn:
# here if isbn we create that dictionary to tell
# mongo which value to modify on which key
keys_to_update["$set"].update({
key: value
})
# simple and old pythonic dict update
if isbn:
# here we update the document book with new values
# from the above generated dict
burtbook.update_one(book, keys_to_update)
# just like the save()
# save is deprecated boys, cant use it anymore ...
else:
book['add_released'] = int(time.time())
burtbook.insert_one(book)
self.redirect("/recommended/")
okey,为我辩护,我无法测试你的代码,但我已经在我的项目中测试了相同的技术并且它知道它对我有用。
告诉我它是否有效。步伐
编辑
另一个有用的reference
你也可以试试这个代码(在这里你可以用你想要的任何东西替换整个文档):
todos_collection = get_collection(todos_collection_name)
requested_todo = todos_collection.find_one({
"_id": ObjectId(oid)
})
todos_collection.replace_one(requested_todo, {"something": "else"})
# this will replace the entire document `request_todo` with `{"something": "else"}`
# but it will keep the original `ObjectID`
# and thats is very useful