Arrays Mongoose从集合中的数组中删除元素

Arrays Mongoose从集合中的数组中删除元素,arrays,mongoose,collections,Arrays,Mongoose,Collections,我有一个这样的用户集合 { "user": { "_id": "6073fdff72556c02e8fb4509", "email": "XXX", "admin": "XXX", "feature": [ { "_id": "60645

我有一个这样的用户集合

{
"user": {
    "_id": "6073fdff72556c02e8fb4509",
    "email": "XXX",
    "admin": "XXX",
    "feature": [
        {
            "_id": "6064579bc6b0510174c1395d",
            "brand": "XXX",
            "category": "XXX",
            "parent": "XXX",
            "features": "XXX",
            "createdAt": "2021-03-31T11:06:03.162Z",
            "updatedAt": "2021-03-31T11:06:03.162Z",
            "__v": 0
        },
        {
            "_id": "60645a1b213dea021c44e762",
            "brand": "XXX",
            "category": "XXX",
            "parent": "XXX",
            "features": "XXX",
            "createdAt": "2021-03-31T11:16:43.729Z",
            "updatedAt": "2021-03-31T11:16:43.729Z",
            "__v": 0
        },
        {
            "_id": "6066ed63db689b02a289d90f",
            "brand": "XXX",
            "category": "XXX",
            "parent": "XXX",
            "features": "XXX",
            "createdAt": "2021-04-02T10:09:39.322Z",
            "updatedAt": "2021-04-02T10:09:39.322Z",
            "__v": 0
        },
        {
            "_id": "606775e2fc48c7023e0a85e6",
            "brand": "XXX",
            "category": "XXX",
            "parent": "XXX",
            "features": "XXX",
            "createdAt": "2021-04-02T19:52:02.072Z",
            "updatedAt": "2021-04-02T19:52:02.072Z",
            "__v": 0
        },
        {
            "_id": "60677b6afc48c7023e0a85e7",
            "brand": "XXX",
            "category": "XXX",
            "parent": "XXX",
            "features": "XXX",
            "createdAt": "2021-04-02T20:15:38.038Z",
            "updatedAt": "2021-04-02T20:15:38.038Z",
            "__v": 0
        }
    ],
    "__v": 0
}
const mongoose = require('mongoose')
const bcrypt = require('bcrypt')
const { isEmail, isStrongPassword } = require('validator') 

    const Schema = mongoose.Schema
    
    const userSchema = new Schema({
        email: {
            type: String,
            required: [true, 'Please enter a valid email'],
            unique: true,
            lowercase: true,
            validate: [isEmail, 'Please enter a valid email']
        },
        password: {
            type: String,
            required: [true, 'Please enter a valid password'],
            validate: [isStrongPassword, 'At least 8 characters, 1 uppercase, 1 lowercase, 1 number, 1 symbol']
        },
        role: {
            type: String,
            required: true,
            lowercase: true
        },
        features: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Feature'
        }
    })
//fire a function to hash password before save to db
userSchema.pre('save', async function (next) {
    const salt = await bcrypt.genSalt()
    this.password = await bcrypt.hash(this.password, salt)
    next()
})

//static method to login user
userSchema.statics.login = async function(email, password) {
    const user = await this.findOne({ email })
    if (user) {
        const auth = await bcrypt.compare(password, user.password)
        if (auth) {
            return user
        }
        throw Error('Incorrect Password')
    }
    throw Error ('Incorrect Email')
}

const User = mongoose.model('user', userSchema)
module.exports = User;
}

此集合通过如下模式填充

{
"user": {
    "_id": "6073fdff72556c02e8fb4509",
    "email": "XXX",
    "admin": "XXX",
    "feature": [
        {
            "_id": "6064579bc6b0510174c1395d",
            "brand": "XXX",
            "category": "XXX",
            "parent": "XXX",
            "features": "XXX",
            "createdAt": "2021-03-31T11:06:03.162Z",
            "updatedAt": "2021-03-31T11:06:03.162Z",
            "__v": 0
        },
        {
            "_id": "60645a1b213dea021c44e762",
            "brand": "XXX",
            "category": "XXX",
            "parent": "XXX",
            "features": "XXX",
            "createdAt": "2021-03-31T11:16:43.729Z",
            "updatedAt": "2021-03-31T11:16:43.729Z",
            "__v": 0
        },
        {
            "_id": "6066ed63db689b02a289d90f",
            "brand": "XXX",
            "category": "XXX",
            "parent": "XXX",
            "features": "XXX",
            "createdAt": "2021-04-02T10:09:39.322Z",
            "updatedAt": "2021-04-02T10:09:39.322Z",
            "__v": 0
        },
        {
            "_id": "606775e2fc48c7023e0a85e6",
            "brand": "XXX",
            "category": "XXX",
            "parent": "XXX",
            "features": "XXX",
            "createdAt": "2021-04-02T19:52:02.072Z",
            "updatedAt": "2021-04-02T19:52:02.072Z",
            "__v": 0
        },
        {
            "_id": "60677b6afc48c7023e0a85e7",
            "brand": "XXX",
            "category": "XXX",
            "parent": "XXX",
            "features": "XXX",
            "createdAt": "2021-04-02T20:15:38.038Z",
            "updatedAt": "2021-04-02T20:15:38.038Z",
            "__v": 0
        }
    ],
    "__v": 0
}
const mongoose = require('mongoose')
const bcrypt = require('bcrypt')
const { isEmail, isStrongPassword } = require('validator') 

    const Schema = mongoose.Schema
    
    const userSchema = new Schema({
        email: {
            type: String,
            required: [true, 'Please enter a valid email'],
            unique: true,
            lowercase: true,
            validate: [isEmail, 'Please enter a valid email']
        },
        password: {
            type: String,
            required: [true, 'Please enter a valid password'],
            validate: [isStrongPassword, 'At least 8 characters, 1 uppercase, 1 lowercase, 1 number, 1 symbol']
        },
        role: {
            type: String,
            required: true,
            lowercase: true
        },
        features: {
            type: mongoose.Schema.Types.ObjectId,
            ref: 'Feature'
        }
    })
//fire a function to hash password before save to db
userSchema.pre('save', async function (next) {
    const salt = await bcrypt.genSalt()
    this.password = await bcrypt.hash(this.password, salt)
    next()
})

//static method to login user
userSchema.statics.login = async function(email, password) {
    const user = await this.findOne({ email })
    if (user) {
        const auth = await bcrypt.compare(password, user.password)
        if (auth) {
            return user
        }
        throw Error('Incorrect Password')
    }
    throw Error ('Incorrect Email')
}

const User = mongoose.model('user', userSchema)
module.exports = User;
我正在尝试从用户集合中的未来阵列中删除id。Thius是我正在使用的控制器

const delete_menu = (req,res) => {
    const featureId = new Feature(req.body)
    console.log(featureId._id);
    User.updateMany({}, {$pull:  {feature: [featureId.id]}}, (err, result) => {
            if (err) return console.log(err)
            res.json(result)

    })
}
我尝试了几种方法,但alkways得到了这种响应,并且收集没有受到影响。我还在学习,所以可能我正在写一篇重要的文章

    {
    "ok": 0,
    "n": 0,
    "nModified": 0
}

这回答了你的问题吗?只需更正您的查询{$pull:{feature:{{u id:featureId.{u id}}}即可重播。已经尝试过链接解决方案,不幸的是它不起作用。feature字段是,一旦请求进入,将从其他集合中弹出Id数组。您只需要从
features
数组中删除该Id,对吗?是的,因为我已经从feature集合中记住了该功能本身。我本来想用猫鼬的post钩子,但一旦我收到响应,种群就已经发生了,猫鼬就从响应中删除了这一特性。另一方面,该id仍然存在于mongodb用户集合和特征字段数组中。