-1
是否可以将key:value
附加到MongoDB游标?如何将key:value附加到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 }]
编辑:我尝试以下作为一种替代,也得到了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)
为什么不把它分成2:'result = {my_message:'value here,data:cursor}'? – WiredPrairie
这是可行的(用逗号分隔键),但它意味着在前端数组迭代时,而不是使用'$ .each(results ...',我使用'$ .each(results.data'。I 'd仍然有兴趣知道为什么我不能直接追加到游标(我假设这是一个字典?)。 – user1063287
条目是一个列表,而不是一个散列 - 所以当你调用'转储时它会返回一个列表''''我想象一下,它不会在列表中包含你在运行时放在对象上的键值 – WiredPrairie