【问题标题】:how to update the single value in an array of objet with fetching object in the array by its id and how to fetch a single value from it如何通过 id 获取数组中的对象来更新对象数组中的单个值以及如何从中获取单个值
【发布时间】:2021-07-23 12:07:28
【问题描述】:
var agentSchema = new Schema({
    datejoined: Date,
     Agent: [{
            name: String,
            phone: String,
            uniqueid: String,
            state: String,
            district: String,
            fleet: String,
            payment: String,
            status: String,
            hint: String
    
        }],})

这是我的猫鼬数据库代码 我想通过 id 访问对象代理数组并单独更新付款状态、命中、舰队等

 Agent.update({ _id: req.query.person_id }).select({ agent: { $elemMatch: { _id: req.query.object_id } } }).then(



    res.redirect('/admin/agentrequest')).catch(err => console.log(err));

我试着像这样获取它,但我无法更新值

                                           <% for (let doc of prods) { %>
                                            <form method="POST" action="/admin/editbus" class="user">
                                             
                                           

               <div class="form-group row">
                                                    <div class="col-sm-12 mb-3 mb-sm-0">
                                                        <label for="status">status</label>

                                                        <input type="text" name="status" class="form-control form-control-user" list="status" id="exampleFirstName" placeholder="<%= doc.status %>">
                                                        <datalist id="status">
                                                            <option value="Pending">
                                                            <option value="Accepted">
                                                            <option value="Rejected">
                                                            <option value="Waiting for change">
                                                            <option value="Verifying">
                                                          </datalist>

                                                    </div>

                                                </div>
                                                <input type="hidden" name="_id" value="<%= doc._id %>">

                                                <input type="submit" value="change" class="btn btn-primary btn-user btn-block col-sm-12 mb-3 mb-sm-0">

                                                </input>
                                            </form>
                                            <% } %>
                                                <hr>

这是我的 ejs

 Agent.update({ 'agent._id': req.body.id }, {
        '$set': {
            'agent.$.phone': req.body.phone,

        }
    }).then(

        res.redirect('/admin/agentrequest')).catch(err => console.log(err));

我也试过这种方式 在此输入代码

【问题讨论】:

  • 最后一个查询看起来不错,你遇到了什么错误?尝试在 async 中包装函数并将 await 放在查询 await Agent.update(.. 之前

标签: node.js mongoose mongoose-schema mongoose-populate


【解决方案1】:
 Agent.findOneAndUpdate({ _id: req.body.agent_id, bus: { $elemMatch: { _id: req.body.object_id } } }, {
        $set: {
            'agent.$.phone': req.body.phone,
            'agent.$.uniqueid': req.body.registration,
            
        }
    }, // list fields you like to change
    { 'new': true, 'safe': true, 'upsert': true }).then(

谢谢你turvishal,但这段代码对我有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 2021-11-24
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2021-12-06
    相关资源
    最近更新 更多