简体   繁体   English

MongoDB使用GraphQL查找和修改

[英]MongoDB Find and Modify With GraphQL

I am working on GraphQL mutation and need help here. 我正在研究GraphQL突变,在这里需要帮助。 My document looks like 我的文件看起来像

{
    "_id" : ObjectId("5bc02db357146d0c385d4988"),
    "item_type" : "CategoryMapping",
    "id" : null,
    "CategoryGroupName" : "Mystries & Thriller",
    "CustomCategory" : [ 
        {
            "name" : "Private Investigator",
            "MappedBisacs" : [ 
                "investigator", 
                "Privately owned", 
                "Secret"
            ]
        },
{
            "name" : "Crime Investigator",
            "MappedBisacs" : [ 
                "crime investigator", 
                "crime thriller"
            ]
        }
    ]
}

UI UI

  • Allow user to update MappedBisacs through list of checkbox. 允许用户通过复选框列表更新MappedBisacs。 So user can add/update or delete list of bisacs. 因此,用户可以添加/更新或删除bisac列表。

Problem - When client send GraphQL query like following; 问题-当客户端发送GraphQL查询时,如下所示;

mutation {
  CategoryMapping_add(input: {CategoryGroupName: "Mystries & Thriller", CustomCategory: [{name: "Crime Investigator", MappedBisacs: ["investigator", "dafdfdaf", "dafsdf"]}]}) {
    clientMutationId
  }
}

I need to find Specific custom category and update its bisac array. 我需要找到特定的自定义类别并更新其bisac数组。

I am not sure if I got it, but this more a doubt on MongoDb than on GraphQL itself. 我不确定是否能做到这一点,但这对MongoDb不是对GraphQL本身是一个疑问。 First you must find the document that you want (I would use the id of the document instead of CategoryGroupName ), then you can update this array in several ways. 首先,您必须找到所需的文档(我将使用文档的id而不是CategoryGroupName ),然后可以通过几种方式更新此数组。 For example, after you found the document, you could simply access the array content and spread into a new one adding this new data from your mutation, and save this object with the update method. 例如,找到文档后,您可以简单地访问数组内容并散布到一个新的数组中,以添加来自您的突变的新数据,然后使用update方法保存该对象。 (if you simply want to add new data without removing any) So, it depends on the case. (如果您只是想添加新数据而不删除任何数据),则取决于情况。

Check: https://docs.mongodb.com/manual/reference/operator/update-array/ 检查: https//docs.mongodb.com/manual/reference/operator/update-array/

Hope it helps! 希望能帮助到你! :) :)

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

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