【问题标题】:how do i remove array of elements in mongodb如何删除mongodb中的元素数组
【发布时间】:2019-05-30 17:44:10
【问题描述】:

我正在尝试删除元素数组,但它根本不起作用..

在架构中

var connectedUsers = Schema({
  fruits: { type: Array },
  vegetables: { type: Array }
})
var connectedusers = mongoose.model("connectedusers", connectedUsers) 

节点js路由文件

router.post('/connectedusers', function(req,res) {
connection.connectedusers.update(
        { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } },
        { multi: true }
    )
    connection.connectedusers.find({}, function (err, docs) {
        if (err) throw err;
        res.json({
            docs: docs
        })
    })
});

在mongodb集合中

{
    "_id": {
        "$oid": "5cef68f690a42ba057760e98"
    },
    "__v": 0,
    "connectArray": [
        "vinay",
        "vinay1"
    ],
    "fruits": [
        "apples",
        "pears",
        "oranges",
        "grapes",
        "bananas"
    ],
    "vegetables": [
        "carrots",
        "celery",
        "squash",
        "carrots"
    ]
}

元素数组未删除。它显示了所有集合详细信息。 如何使用 $Pull 或任何其他方法从 mongodb 中删除元素。

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-query


    【解决方案1】:

    您忘记了匹配查询,数据库函数为asynchronous,因此您需要await 才能完成更新操作,然后再查询以检查数据库是否已更改,这可以通过async functions 完成

    // change callback to an async arrow function
    router.post('/connectedusers', async (req, res) => {
        try {
            // wait for this operation to complete before continuing
            await connection.connectedusers.update(
                {},
                { $pull: { fruits: { $in: [ "apples", "oranges" ] }, vegetables: "carrots" } },
                { multi: true }
            );
            // update complete now wait for the find query
            const docs = await connection.connectedusers.find({});
            // no errors, send docs.
            // if the variable name is the same as your field
            // that you want to send in the object you can just
            // pass that variable directly
            res.json({ docs });
        // catches errors from both update and find operations
        } catch (err) {
            // set the status code and send the error
            res.status(/* error status */).json(err);
        }
    });
    

    【讨论】:

      【解决方案2】:

      尝试如下:

      router.post('/connectedusers', function(req,res) {
      connection.connectedusers.update(
              {},                   //  Missing query part
              { 
                 $pull: { 
                       fruits: { $in: [ "apples", "oranges" ] }, 
                       vegetables: "carrots" 
                 } 
              },
              { multi: true }
          )
          connection.connectedusers.find({}, function (err, docs) {
              if (err) throw err;
              res.json({
                  docs: docs
              })
          })
      });
      

      您的查询运行良好,我在最后尝试过。您缺少来自 MongoDB update 函数的查询部分

      【讨论】:

        猜你喜欢
        • 2013-06-02
        • 2016-02-25
        • 2020-04-16
        • 1970-01-01
        相关资源
        最近更新 更多