【问题标题】:How to add fields in MongoDb document which already exist?如何在已经存在的 MongoDb 文档中添加字段?
【发布时间】:2020-04-10 03:33:27
【问题描述】:

我正在尝试使用 Postman 向 MongoDb 获取一些 POST 请求,并且一切正常。

以下是代码:

def add_npo():

add_new_npo = mongo.db.npos

name = request.json['name']   
description = request.json['description']   
category = request.json['category']  
status = request.json["status"]
npo_id = add_new_npo.insert({'name': name, 'description':
description, 'category': category,'status': status})

new_npo = add_new_npo.find_one({'_id': npo_id })
output = {'name': new_npo["name"], 'description':
new_npo["description"], 'category': new_npo["category"], 'status':
new_npo["status"]}
return jsonify({'result' : output})

但是如何在不预先分配的情况下在文档中添加新字段?

【问题讨论】:

    标签: python mongodb flask postman pymongo


    【解决方案1】:

    您可以通过update操作在文档上添加新字段。

    例如,

    db.col.update({your filter}, {"$set": {"newField": newFieldValue}})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 2020-08-09
      • 1970-01-01
      • 2016-10-24
      • 2014-02-14
      • 2019-07-24
      • 2020-02-23
      相关资源
      最近更新 更多