Reputation: 737
db.categories.remove(
{"categoryId" : 510001,"name" : "Nevresim Takımları"},{
productTypes:{$elemMatch:{name:"Seri Sonu Ürünler"}}})
I used this for
"_id" : ObjectId("57e6fb9ca71845002b9be169"),
"_class" : "net.infoowl.hepsiburada.microservice.product.domain.Category",
"name" : "Nevresim Takımları",
"categoryId" : 510001,
"parentCategoryId" : 9010109,
"productTypes" : [
{
"_id" : ObjectId("57e6fa87e0c6de002c209124"),
"name" : "Seri Sonu Ürünler",
"requiredHeaders" : [ ],
"uniqueConstraints" : [ ],
"fields" : [ ],
"createdAt" : ISODate("2016-09-24T22:13:27.882Z"),
"createdBy" : "user-0",
"modifiedAt" : ISODate("2016-09-24T22:13:27.882Z"),
"modifiedBy" : "user-0",
"extend" : DBRef("productTypes", "base")
to delete that "seri sonu ürünler"
but it all deleted
"categoryId" : 510001,"name" : "Nevresim Takımları"
why?
When i did find, it found correct variable in array but when remove, why different?
db.categories.find({"categoryId" : 510001,"name" : "Nevresim Takımları"},{productTypes:{$elemMatch:{name:"Seri Sonu Ürünler"}}}).pretty()
{
"_id" : ObjectId("57e6fb9ca71845002b9be169"),
"productTypes" : [
{
"_id" : ObjectId("57e6fa87e0c6de002c209124"),
"name" : "Seri Sonu Ürünler",
"requiredHeaders" : [ ],
"uniqueConstraints" : [ ],
"fields" : [ ],
"createdAt" : ISODate("2016-09-24T22:13:27.882Z"),
"createdBy" : "user-0",
"modifiedAt" : ISODate("2016-09-24T22:13:27.882Z"),
"modifiedBy" : "user-0",
"extend" : DBRef("productTypes", "base")
}
]
}
this is for remove
db.categories.remove(
{"categoryId" : 510001,"name" : "Nevresim Takımları"},{
productTypes:{$elemMatch:{name:"Seri Sonu Ürünler"}}})
this is for find
db.categories.find({"categoryId" : 510001,"name" : "Nevresim Takımları"},{productTypes:{$elemMatch:{name:"Seri Sonu Ürünler"}}}).pretty()
Upvotes: 1
Views: 73
Reputation: 2539
remove
is used to remove full documents from the collection using a given query. Your query returned this document so it was all deleted.
If you want to delete an element from an array inside a document, you need to use update
with $pull
In you case, you would want to do it like this:
db.categories.update(
{
"categoryId" : 510001,
"name" : "Nevresim Takımları"
},
{
$pull: {
productTypes: {
name: "Seri Sonu Ürünler"
}
}
}
)
Upvotes: 2