Create schema for dictionary in app service

Hello!
I have a collection which stores python dict. I want to build a schema in app service for graphQL. My dict looks like this:

"job_list": {
    "job_1: {
        info: {
            "a":1,
           "b":2,
        }
    },
    "job_2: {
        info: {
            "p":111,
           "q":12,
        }
    },
    "job_3: {
        info: {
            "x":11,
           "y":22,
        }
    }
}

Thanks

2 Likes

This doc says that I can create dict type by not defining property field and setting additionalProperty field. But when I do that I get a warning “schema must have a “properties” field” and then I can’t access that particular attribute while querying in graphql

2 Likes

I have a simmiliar issue. I try to define dictionary in schema as it decribed in docs:

    "consumed": {
      "bsonType": "object",
      "additionalProperties": {
        "bsonType": "string"
      }
    },

but I get error: schema must have a “properties” field

Hi Edgar,

The schema format should look like this:

{
  "bsonType": "object",
  "title": "<Type Name>",
  "required": ["<Required Field Name>", ...],
  "properties": {
    "<Field Name>": <Schema>
  }
}

It sounds like you’re missing the properties field as shown, Is this present?

Regards
Manny

Hi, well just adding properties like this:

"test": {
  "bsonType": "object",
  "properties": {}
}

still gives error: schema must have a “properties” field
adding a a field in properties works:

"test": {
  "bsonType": "object",
  "properties": {
    "test1": {
      "bsonType": "string"
    }
  }
}

but this will not give a dictionary. Desired result is a collection of dynamic and unique string keys paired with values of a given type as described in docs

Hi Edgar,

Could you share your app id?

Here:

example-qjcct

I have made a new app with separate cluster to demonstrate it. Technically I decided to change my data structure in a way that I no longer need this feature, but it still could be interesting to figure it out for a future reference.

I’ve hit the same issue

Schema looks like:

{
  "title": "Guest",
  "properties": {
    "_id": {
      "bsonType": "objectId"
    },
    "createdAt": {
      "bsonType": "date"
    },
    "createdById": {
      "bsonType": "objectId"
    },
    "attributes": {
      "name": "attributes",
      "bsonType": "object",
      "additionalProperties": true
    }
  },
  "required": [
    "_id",
    "createdAt",
    "createdById",
  ]
}

This results in the GraphQL error against attributes: schema must have a “properties” field.

If I modify the scheme to:

"attributes": {
      "name": "attributes",
      "bsonType": "object",
      "properties": {
        "extraStuff": {
          "name": "extraStuff",
          "bsonType": "string"
        }
      },
      "additionalProperties": true
    }

Then I’ll get a sync error “object property “attributes” has invalid subschema: object cannot have additionalProperties in addition to existing properties”. Which makes sense but I don’t seem to be able to find a way to satisfy the GraphQL error…

Edit: I will likely change the solution over to this suggestion from a similar thread: Schema not working as describe on documentation - #4 by Anuj_Garg

@Graeme_Maciver1 Welcome to MongoDB Community.

Yes. Dictionary property is is not allowed in API Schema.

1 Like