このモデルを考えると、どのようにupdateOne(マングース)とネストされた配列の項目を削除するには?

Olavo Alexandrino:

私は「updateOne」メソッドを使って配列の項目を削除しようとしていますが、私のクエリは、私が持っているモデル構造の右のレコードに一致していません。メールを考えると、私は提供メールで配列項目を見つけて、それを引き出し、それを削除したいと思います。(同じ電子メールとは配列項目はありません)

私のモデルはそうのようなものです:

var mongoose = require('mongoose');

var teamMemberModelSchema = new mongoose.Schema({
     _id: false,
    "email": {
        "type": String,
        "required": true,
        "minlenght": 5,
        "maxheight": 50
    },
    "name": {
        "type": String,
        "required": true,
        "minlenght": 5,
        "maxheight": 256
    },
    "role": {
        "type": String,
        "required": true,
        "minlenght": 20,
        "maxheight": 256
    },
    "twitter": {
        "type": String,
        "required": true,
        "minlenght": 1,
        "maxheight": 100
    },
    "facebook": {
        "type": String,
        "required": true,
        "minlenght": 1,
        "maxheight": 100
    },
    "linkedin": {
        "type": String,
        "required": true,
        "minlenght": 1,
        "maxheight": 100
    },
});

var teamModelSchema = new mongoose.Schema({
    "title": {
        "type": String,
        "required": true,
        "minlenght": 5,
        "maxheight": 20
    },
    "headline": {
        "type": String,
        "required": true,
        "minlenght": 5,
        "maxheight": 30
    },
    "description": {
        "type": String,
        "required": true,
        "minlenght": 5,
        "maxheight": 80
    },
    "members": [teamMemberModelSchema]
}, { collection: 'team' });

teamModelSchema.set('collection', 'team');
mongoose.model('team', teamModelSchema)

そして、私がしようとしているというアプローチは以下の通りであります:

module.exports.removeMember = function (req, res) {

    const email = req.params.email;
    const query = { "members.email": email };
    const pull = { $pull: { "members.$.email": email } };

    try {

        var message = teamMsg.teamMemberRemoveSuccess;

        TeamModel.updateOne( query, pull);


        responseUtilities.sendJSON(res, false, { "message": message });

    } catch (err) {
        console.log(err.message);
        responseUtilities.sendJSON(res, err, { "message": err.message });
    }
};

これは、エラーなしで実行されますが、何も更新されません。

私は解決策を見つけることができませんでした、「FindOneAndUpdate」と「FindOneAndRemove」と他のいくつかの選択肢を試みたが、しています。

何か案は?

SuleymanSah:

あなたは使うことができfindOneAndUpdate$pull、このタスクのための演算子。

文書の配列から項目を削除するために、あなたはのMongoDB確認することができますドキュメントを

あなたは使用する必要がありますawaitまたはthenクエリにブロックします。私はのawaitを使用して、追加することで機能を非同期に作られたasyncキーワードを。また、空のクエリオブジェクトを必要としています。

私はまた、追加new: trueの項目が削除されたかどうかを確認するために更新されたオブジェクトを返すために、オプションを選択します。

あなたは私があなたのためのTODOを追加していない文書の試合、ケースを処理する必要があります。

module.exports.removeMember = async function(req, res) {
  const email = req.params.email;
  const query = {};

  const pull = {
    $pull: {
      members: {
        email: email
      }
    }
  };

  const options = {
    new: true
  };

  try {
    var message = teamMsg.teamMemberRemoveSuccess;

    const result = await TeamModel.updateOne(query, pull, options);

    console.log(result);

    if (!result) {
      //TODO: return 400-Bad Request or 404 - Not Found
    } else {
      responseUtilities.sendJSON(res, false, { message: message });
    }
  } catch (err) {
    console.log(err.message);
    responseUtilities.sendJSON(res, err, { message: err.message });
  }
};

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=294612&siteId=1