1
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()

1
  • Your question is hard to understand. What are you trying to do what is the expected result. Commented Oct 6, 2016 at 7:49

1 Answer 1

2

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"
            }
        }
    }
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.