1

My MongoDB Data looking like Below:-

      {
      "_id": 1,
      "categories":[
      {
      "categoryName": "Automobiles",
      "categoryId": 1,
      "catDisplayName": "language1",
      "subCategories": [
        {
            "subCategoryName": "cars",
            "subCategoryId": 1,
            "subCatDisplayName": "language2"
        },
        {
            "subCategoryName": "bykes",
            "subCategoryId": 2,
            "subCatDisplayName": "language3"
        }

    ]
},
{
    "categoryName": "Electronics",
    "categoryId": 2,
    "catDisplayName": "somelanguage1",
    "subCategories": [
        {
            "subCategoryName": "Mobiles",
            "subCategoryId": 1,
            "subCatDisplayName": "somelanguage2"
        },
        {
            "subCategoryName": "cameras",
            "subCategoryId": 2,
            "subCatDisplayName": "somelanguage3"
        }
       ]
      }
      ]
      }

We are trying two cases with MongoDb Casbah the following cases are:-

  1. if catagory exists and subCategory doesn't exist then update the subcategory in subCategories Array

  2. if catagory doesn't exist and subCategory doesn't exist then update the category and subCategory in categories and subCategories Array Respectively

My Case 1 code looking like this it is working fine

val builderObj = MongoDBObject.newBuilder
builderObj += "subCategoryName" -> "cycles"
builderObj += "subCategoryId" -> 3
builderObj += "subCategoryName" -> "language4"
builderObj.result ()

val query = MongoDBObject("categories.categoryName" -> "Automobiles")

val updatingdata = $addToSet("categories.$.subCategories" -> builderObj.result ())
collection.update(query,updatingdata,true,true)

But How to Write For Case 2 We are trying like below

val builderObj1 = MongoDBObject.newBuilder
builderObj1 += "categoryName"-> "Entertainment"
builderObj1 += "categoryId"-> 3
builderObj1 += "catDisplayName"-> "somelanguage4"

val builderObj = MongoDBObject.newBuilder
builderObj += "subCategoryName" -> "Tickets"
builderObj += "subCatDisplayName"-> "language1"
builderObj += "subCategoryId" -> 1
builderObj.result ()

val query = MongoDBObject("categories.$.categoryName" -> "Entertainment") //Entertainment  Category doesn't exist in my data

val updatingdata = ? // How To Update the Cateroy and SubCategory at a time 
collection.update()

Please give me Suggestions i am struck here

Best Regards GSY

jcern
  • 7,798
  • 4
  • 39
  • 47
sagar
  • 143
  • 1
  • 2
  • 11

1 Answers1

1

Since you are creating a new category, the subCategories array of the new category will be empty. So you can just add it to the new category directly.

val subCatBuilder = MongoDBObject.newBuilder
subCatBuilder += "subCategoryName" -> "Tickets"
subCatBuilder += "subCatDisplayName"-> "language1"
subCatBuilder += "subCategoryId" -> 1
subCatBuilder

val catBuilder = MongoDBObject.newBuilder
catBuilder += "categoryName"-> "Entertainment"
catBuilder += "categoryId"-> 3
catBuilder += "catDisplayName"-> "somelanguage4"
catBuilder += "subCategories" -> MongoDBList(subCatBuilder.result())

val query = MongoDBObject() // How do you know which elements of collection to update?

val updatingdata = $push("categories" -> catBuilder.result())

collection.update(query, updatingdata, true, true)

Of course this will update every element of collection to add the new category. If you want to be more selective, you'll need to update query appropriately.

kong
  • 1,961
  • 16
  • 16
  • Hi..... Kong the above code working fine But we want to Delete The subCategory(Ex:-i want to Delete Mobiles from mydata) form Electronics category we are Trying like below code:- val removingData = $pull(MongoDBObject("categories" -> MongoDBObject("categoryName" -> "Entertainment"))) this code is for removing Particular category But How To Remove ONE or MORE subCategories from particular category. – sagar Dec 03 '13 at 11:25
  • I'm not sure I fully understand what you want to do. Perhaps you could submit a separate question and expand a bit more on what you want to do and what you've tried so far. – kong Dec 03 '13 at 14:13