【问题标题】:MongoDB Find and Modify With GraphQLMongoDB 使用 GraphQL 查找和修改
【发布时间】:2019-03-24 13:13:48
【问题描述】:

我正在研究 GraphQL 突变,在这里需要帮助。我的文档看起来像

{
    "_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"
            ]
        }
    ]
}

用户界面

  • 允许用户通过复选框列表更新 MappedBisac。因此用户可以添加/更新或删除 bisacs 列表。

问题 - 当客户端发送如下 GraphQL 查询时;

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

我需要找到特定的自定义类别并更新其 bisac 数组。

【问题讨论】:

    标签: mongodb graphql


    【解决方案1】:

    我不确定我是否明白了,但这更多是对MongoDb 的怀疑,而不是对GraphQL 本身的怀疑。首先您必须找到您想要的文档(我会使用文档的id 而不是CategoryGroupName),然后您可以通过多种方式更新此数组。例如,找到文档后,您可以简单地访问数组内容并传播到一个新的数组中,添加来自您的变异的新数据,并使用update 方法保存此对象。 (如果您只是想添加新数据而不删除任何数据) 所以,视情况而定。

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

    希望对您有所帮助! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-13
      • 2019-07-14
      • 2013-11-05
      • 2015-09-07
      • 2021-05-18
      • 1970-01-01
      • 2020-04-14
      • 2021-09-09
      相关资源
      最近更新 更多