Pymongo 通过 ObjectId 更新记录

问题描述 投票:0回答:1

当我尝试使用 MongoDB 中的 _id 然后更新记录时,我得到以下信息:

id: "{'$oid': '643c1049138324bd119ab59b'}"

我想将 643c1049138324bd119ab59b 部分从完整表达式中分离出来,以便将其发送到我的 API。

事实上,我希望能够传递一个带有正确 ObjectId 的变量,而不仅仅是硬编码 ObjectId

到目前为止看起来是这样的。但是当我将 id 传递给“query”:id 以访问 api 时,没有任何更新。

@app.route('/update/<id>/<blogtitle>/<blogpost>', methods=['GET','POST'])  
def update(id, blogtitle, blogpost):  
    try:  
        if request.method == 'GET':  
            return render_template('update.html', title='Update Record', blogtitle=blogtitle, old_post=blogpost)  
        if request.method == 'POST':  
            blogpost = request.form['blogpost']    
            url = 'http://localhost:7071/api/UpdateRecord'  
            x=requests.post(url, params={"query":id,"new_data":blogpost})  

此尝试无效:

@app.route('/update/<id>/<blogtitle>/<blogpost>', methods=['GET','POST'])  
def update(id, blogtitle, blogpost):  
    try:  
        if request.method == 'GET':  
            return render_template('update.html', title='Update Record', blogtitle=blogtitle, old_post=blogpost)  
        if request.method == 'POST':  
            blogpost = request.form['blogpost'] 
            new_id=ObjectId(id)   
            url = 'http://localhost:7071/api/UpdateRecord'  
            x=requests.post(url, params={"query":new_id,"new_data":blogpost})  

当我测试硬编码版本时,它确实有效。

    if request.method == 'POST':
        #creates the code to update the info when the submit button is pressed
        blogpost = request.form['blogpost']
        fake_id=ObjectId('643c1049138324bd119ab59b')
        url = 'http://localhost:7071/api/UpdateRecord'
        x=requests.post(url, params={"query":fake_id,"new_data":blogpost})

任何想法我可以做些什么来让所有这些都很好地相互交谈。谢谢。

为清楚起见,我希望能够获得 643c1049138324bd119ab59b 来自“{'$oid':'643c1049138324bd119ab59b'}” 对于 MongoDB 数据库中的任何记录。

守则的其他部分:

对于 Azure 函数 API:

import logging

import azure.functions as func
import data_manager

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed an update request.')

    query_string = req.params.get('query') #MongoDB _id
    new_data_string = req.params.get('new_data') #the updated blogpost

    data_manager.initialize()
    updated_record=data_manager.update({'_id':query_string}, {'blogpost': new_data_string})
    if updated_record:
        return func.HttpResponse(status_code=200)
    else:
        return func.HttpResponse(status_code=500)

data_manager.py文件中

def update(query, new_values):
    global mycol
    try:
        mycol.update_many(query, {'$set': new_values})
    except Exception as e:
        print(e)
        return False
    else:
        return True

和 html 文件:

{% extends 'base.html' %}

{% block content %}
<div class="posts">
    <div class="post">
        <div class="section">     
        <h1>Update Your Message</h1>
        <a href="{{ url_for('index') }}">Index</a> > Update Record
        <p>
        <h1>Original Title: {{ blogtitle }}</h1>
        <!--Creates a form to update the blogpost, prefills the form with the old info-->
        <form method="post">         
            <label for="Message">Blog Post:</label>
            <br>
            <textarea name="blogpost",
            maxlength="1000",
            rows="5",
            cols="80",value="{{ request.form['blogpost'] }}">{{ old_post }}</textarea>
            <br>
            <button type="submit">Submit</button>
        </form></p>
        </div>
    </div>
</div>    
{% endblock %}
flask pymongo objectid
1个回答
0
投票

在 pymongo 中,您可以使用内置的

str()
获取 ObjectId 值。

id_str = str(obj_id)
© www.soinside.com 2019 - 2024. All rights reserved.