employeeStructureSchema.js
var employeeStructureSchema = new Schema({
name: String
});
module.exports = mongoose.model('employeeSturctureModel', employeeStructureSchema);
companySchema.js
var employeeStructureSchema = require('./employeeStructureSchema.js').schema;
var companySchema = new Schema({
companyName: String,
phone: String,
email: { type: String, lowercase: true },
companyAddress: String,
settings: {
"employees": [employeeSchema],
},
});
module.exports = mongoose.model('companies', companySchema);
我的目标是删除一些名字以"虚拟用户"来自settings.employees
我试过以下查询。这是工作。你可以建议吗
db.companies.update({"_id":objectId},{$pull:{"settings.employees":{ $elemMatch: {name: /^dummy user/}}}});
我的单个文件是这样的:
{
"_id" : ObjectId("563b35d1f07cc9d80a26436c"),
"settings" : {
"employees" : [
{
"_id" : ObjectId("583d28253722e240103e0108"),
"name" : "dummy user"
},
{
"_id" : ObjectId("583d282e3722e240103e0109"),
"name" : "dummy user1"
},
{
"_id" : ObjectId("583d28403722e240103e010b"),
"name" : "dummy user2"
}
]
}
}
答案 0 :(得分:0)
假设您的员工姓名是一种列表。您可以尝试这样的查询:
db.companies.update({},{$pull: {settings.employees.name: { $regex: '^dummy user'} }});
由于您的架构受到限制,因此该查询应该可以正常工作
db.companies.update({},{
'$pull': {
'settings.employees': {
'name':{ $regex: '^dummy user'}
}
}
});