[英]How to deleteMany in mongoose
I have a database like this:我有一个这样的数据库:
User {
id: 12323,
name: asdasd,
products: [{ id: 1, name: asdasdl }, {id: 2, name: bfcvc}]
}
With deleteMany
, I want to remove all that have products name 'asdasdl'
.使用deleteMany
,我想删除所有产品名称为'asdasdl'
产品。
How can I do this?我怎样才能做到这一点?
I have this code but it doesn't work:我有这段代码,但它不起作用:
await User.deleteMany({'name': { '$in': User.products }}, {});
Thank you!谢谢!
You can use the $eq operator like this您可以像这样使用$eq 运算符
await User.deleteMany({ 'products.name': { $e: 'asdasdl' }});
By changing the query parameter, you can get all documents with a products.name == 'asdasdl' and then delete them:通过更改查询参数,您可以获得所有带有 products.name == 'asdasdl' 的文档,然后将它们删除:
await User.deleteMany({ 'products' : { 'name': 'asdasdl' }});
See deleteMany docs here and Mongo queries on arrays here请参阅此处的 deleteMany 文档和有关 arrays的 Mongo 查询
Thank you for your answer.谢谢您的回答。
In fact I don't want to delete the Users, I want to delete the products.实际上我不想删除用户,我想删除产品。 So if the name is 'asdasdl', delete that product.因此,如果名称是“asdasdl”,请删除该产品。
Thank you!谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.