我想使用“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"方法的替代方案,但是我没有找到解决办法。
有什么想法吗?
{ "members.email": email }
是否返回结果吗? - Genositemembers
对象,其中{"members.email": email}
,还是只想从members
对象中删除email
字段? - whoami - fakeFaceTrueSoul