【问题标题】:Update data in MongoDB with Moongoose and MEAN Stack使用 Mongoose 和 MEAN Stack 更新 MongoDB 中的数据
【发布时间】:2017-08-30 15:30:32
【问题描述】:

我有一个像这样的猫鼬数据:

{
    "_id" : ObjectId("58e394cacaa6512ed00e8d83"),
    "RoleName" : "Manager",
    "UIList" : [ 
        {
            "Edit" : false,
            "View" : false,
            "UiName" : "ElevatorMapping"
        }, 
        {
            "Edit" : false,
            "View" : false,
            "UiName" : "ElevatorAssumptions"
        }, 
        {
            "Edit" : false,
            "View" : false,
            "UiName" : "ManageUnits"
        }, 
        {
            "Edit" : false,
            "View" : false,
            "UiName" : "ManageBuildings"
        }
    ],
    "__v" : 0,
    "Description" : "fd",
    "IsActive" : true
}

现在我想将 UIname:"ElevatorMapping"&"Elevator Assumptions" View and Edit 中的 UILIST 更新为 True。如何为此编写查询?我尝试过类似下面的方法。

这是我的控制器页面。

 $http.put('/ViewEditupdate/' + id +'/' + uiname + '/' + view + '/' + edit).then(function (response) {

                    refresh();
                })

此处的 id 值被正确采用,但编辑视图 UI 名称未被采用。编辑,视图名称被视为未定义。

我的 server.js 代码

app.put('/ViewEditupdate/:id/:uiname/:view/:edit', RoleEntrypath.RoleViewEditupdate);

我的架构代码

    newRole.update({ "UIList.UiName": "ManageRole" }, { "$push": { "UIList.$.View": "true", "UIList.$.Edit": "true" } },
   function (err, doc) {

        res.json(doc);
    }
    );

谁能给出解决方案?

【问题讨论】:

  • 很难跟进您的问题。你为什么不试着用更简单的术语来表达呢?
  • 嗨 ankit,我已经更新了我的问题
  • 你能打印你的 put API 中的所有参数值吗?首先,让我们看看你是否收到了正确的价值观。如果您收到正确的值,那么问题可能出在 update 查询上。

标签: angularjs node.js mongoose


【解决方案1】:

您应该使用$set 而不是$pushview 的值从false 更改为true

试试这个:

newRole.update({ 
    "UIList.UiName": "ManageRole" 
},{ 
    $set: { "UIList.$.View": "true", "UIList.$.Edit": "true" } 
},function (err, doc) {
    res.json(doc);
});

【讨论】:

    【解决方案2】:

    我已经使用此查询进行了更新。

     newRole.update
            (
            {
            _id: id,
            "UIList.UiName": "ManageRole"
        },
                    {
                        $set: {
                            "UIList.$.View": "true",
                            "UIList.$.Edit": "true"
                        }
                    },
       function (err, doc) {
    
            res.json(doc);
        }
        );
    

    【讨论】:

    • 这与我给出的答案完全相同。您应该已将我的答案标记为正确答案,我为您的代码所遇到的问题提供了解决方案。
    • :@ravi Shankar:我前一天完成了这个查询。这样我就不会被标记为您的答案。并且您的答案中没有提到您的 id 名称
    猜你喜欢
    • 2016-12-11
    • 2014-06-05
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 2017-07-24
    • 2014-09-07
    相关资源
    最近更新 更多