簡體   English   中英

mongoose - 使用 $pull 刪除數組中的元素

[英]mongoose - remove element in array using $pull

我有一個如下所示的節點對象:

{
   "_id":"58b336e105ac8eec70aef159",
   "name":"my node",
   "ip":"192.168.1.1",
   "__v":0,
   "configuration":{
       "links":[
         {
            "linkName":"athena_fw_listen_tcp_5555",
            "_id":"58b336e105ac8eec70aef15d",
            "local":true
         },
         {
            "linkName":"athena_fw_listen_udp_5554",
            "_id":"58b336e105ac8eec70aef15c",
            "local":true
         }
      ]
   }
}

我正在向我的快速服務器發送一個刪除請求,如下所示: DELETE http://localhost:9000/api/nodes/58b336e105ac8eec70aef159/links/58b336e105ac8eec70aef15d

我按照$pull mongodb 文檔中的說明進行操作,我也嘗試了這個

但它似乎不起作用,因為我不斷收到: 500 (Internal Server Error)

這就是我快遞方面的代碼的樣子:

 exports.destroyLink = function(req, res) { Node.findById(req.params.id, function(err, node) { if (err) { return handleError(res, err); } if (!node) { return res.status(404).send('Not Found'); } console.log("Node", JSON.stringify(node)) console.log("Params", req.params) node .update({ '_id': req.params.id }, { $pull: { 'configuration': { 'links': { '_id': req.params.linkId } } } }, false, true) .then(err => { if (err) { return handleError(res, err); } return res.status(204).send('No Content'); }); }) };

快速路由器包含以下內容: router.delete('/:id/links/:linkId', controller.destroyLink);

所以我期望 id 和 linkId 作為參數,我使用 id ( _id: req.params.id ) 來定位特定節點和 linkId ( _id: req.params.linkId ) 來定位特定鏈接,但它不起作用!

需要幫助解決問題,我不知道我在這里缺少什么!

大家好,感謝您的幫助。 我終於讓它工作了!!!

經過近 3 個小時的故障排除 :'( :'( 這是使用的解決方案:

exports.destroyLink = function(req, res) {
Node.findByIdAndUpdate(
    req.params.id, { $pull: { "configuration.links": { _id: req.params.linkId } } }, { safe: true, upsert: true },
    function(err, node) {
        if (err) { return handleError(res, err); }
        return res.status(200).json(node.configuration.links);
    });
};

取而代之的findById然后更新的節點,我沒有同時使用findByIdAndUpdate 它現在完美運行!!!

我仍然沒有找到其他版本的解釋。 但無論如何很高興它以這種方式工作。

我遇到了類似的問題,正在處理像您這樣的查詢。 我認為您的查詢沒有找到您要查找的 ID。 如果你想通過 '_id' 在 mongo 中找到一個對象,它需要作為一個 ObjectId 傳入。 嘗試修改內容,使其看起來像這樣:

const mongo = require('mongodb');
var oId = new mongo.ObjectID(req.params.id);
update({ '_id': oId }, { $pull: { 'configuration': { 'links': { '_id': req.params.linkId } } } }, false, true)

我從來沒有使用 mongodb $pull操作符完成這項工作。 另一種方法是找到文檔,然后使用 mongoose pull方法更新數組。 pull方法可以將一個 id 字符串作為它的單個參數,並且知道如何處理它。 對於這個使用 Promise 的例子,代碼看起來像:

exports.destroyLink = function(req, res) {
  Node.findById(req.params.id)
    .then(node => {
      node.configuration.links.pull(req.params.linkId)
      return node.save()
    .then(node => res.send(node.configuration.links))
}

貓鼬文檔pull

我通過更改為 .update mongoose 函數來工作:

{ $pull: { 'configuration': { 'links': { '_id': ''+req.params.linkId+'' } } } }

一旦你把它連接起來,它就會從數組中拉出來。

對於我的問題,這有效

等待 FormSchema.updateOne({formName: name}, {$pull: {fields: {fieldName: fieldName}}}).exec();

這就是我的架構的樣子

{
"fields": [
    {
        "contentType": "Text",
        "fieldName": "First Name",
        "textType": "short",
        "isUnique": false
    },
    {
        "contentType": "Boolean",
        "fieldName": "Contact Me"
    }
],
"_id": "61f71efd14cafb5a50ba365f",
"formName": "Contact",
"__v": 4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM