简体   繁体   English

如何使用Casbah更新mongoDB中的嵌套Arrayof对象

[英]how to Update nested Arrayof objects in mongoDB using Casbah

My MongoDB Data looking like Below:- 我的MongoDB数据如下所示:-

      {
      "_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:- 我们正在尝试使用MongoDb Casbah的两种情况,以下情况是:

  1. if catagory exists and subCategory doesn't exist then update the subcategory in subCategories Array 如果类别存在并且subCategory不存在,则更新subCategories数组中的子类别

  2. if catagory doesn't exist and subCategory doesn't exist then update the category and subCategory in categories and subCategories Array Respectively 如果类别不存在且subCategory不存在,则分别更新类别和subCategories数组中的category和subCategory

My Case 1 code looking like this it is working fine 我的案例1代码看起来像这样,它工作正常

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 但是如何写案例2我们正在尝试如下

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 最好的问候GSY

Since you are creating a new category, the subCategories array of the new category will be empty. 由于您正在创建新类别,因此新类别的subCategories数组将为空。 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. 当然,这将更新collection每个元素以添加新类别。 If you want to be more selective, you'll need to update query appropriately. 如果您希望更具选择性,则需要适当更新query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM