如何将key:value附加到MongoDB游标?

如何将key:value附加到MongoDB游标?,mongodb,python-2.7,pymongo,Mongodb,Python 2.7,Pymongo,是否可以将键:值附加到MongoDB光标 我试过这个: cursor = collection.find(query,projection) cursor['my_message'] = "value here" # trying to add key:value here 但它似乎不起作用(500) 在更大的背景下,这是可行的: dbname = 'my_db' db = connection[dbname] collection = db.my_collection query = {'k

是否可以将
键:值
附加到MongoDB光标

我试过这个:

cursor = collection.find(query,projection)
cursor['my_message'] = "value here" # trying to add key:value here
但它似乎不起作用(500)

在更大的背景下,这是可行的:

dbname = 'my_db'
db = connection[dbname]
collection = db.my_collection
query = {'key_1': my_var}
projection = {'key_2':1}
cursor = collection.find(query,projection)
response.content_type = 'application/json'
return dumps(cursor)
这并不是:

dbname = 'my_db'
db = connection[dbname]
collection = db.my_collection
query = {'key_1': my_var}
projection = {'key_2':1}
cursor = collection.find(query,projection)
cursor['my_message'] = "value here" # trying to add key:value here
response.content_type = 'application/json'
return dumps(cursor)
编辑:为了直观地显示成功返回的内容(没有附加值),它类似于:

[{ document_1 },{ document_2 },{ document_3 }]
["my_message":"value here",{ document_1 },{ document_2 },{ document_3 }]
我希望它看起来像:

[{ document_1 },{ document_2 },{ document_3 }]
["my_message":"value here",{ document_1 },{ document_2 },{ document_3 }]
编辑:我尝试了以下方法作为替代,也得到了500分

entries = []
cursor = collection.find(query,projection)
for entry in cursor:
    entries.append(entry)
entries['my_message'] = "value here"
response.content_type = 'application/json'
return dumps(entries)
真的,一开始就为你回答了这个问题,每个人都在说同样的话

我们知道你想做什么。您希望您的序列化响应被发送回一些您想要输入的信息,然后返回结果集。我还假设您希望使用这些结果和其他数据,这些数据可能加载到某个JavaScript处理存储中

我从未见过任何不希望出现某种结构的东西,如:

{
   "result": "ok",
   "meta": [{ akey: "avalue"}, {bkey: "bvalue"}],

   "results:[              // this is your 'entries' value here
       { document_1 },
       { document_2 },
       { document_3 },
       ....

所以每个人都在说的是将您的
条目
嵌入到另一个要序列化并返回的结构中。如果试图将其他键推入
条目
列表中,您的做法是错误的。

为什么不将其拆分为2:
result={my_message:'value here',data:cursor}
?这是可行的(在键周围有逗号),但这只意味着在前端的数组上迭代时,而不是使用
$(results…
,我使用
$.each(results.data
)。我仍然想知道为什么我不能直接附加到游标(我假设这是一个dict?).Entries是一个列表,而不是散列--所以我想当你调用
dumps
时它会返回一个列表。它不会在列表中包含你在运行时放在对象上的键值。我想你把列表和dict搞混了。我不明白这个表达式代表什么:
[“我的消息”:“value here”、{document u 1}、{document u 2},{document_3}]
。这是一个列表还是一个目录?