Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrderedDict gives error in Mongoc.BSON() #111

Open
haenf opened this issue Aug 18, 2023 · 1 comment
Open

OrderedDict gives error in Mongoc.BSON() #111

haenf opened this issue Aug 18, 2023 · 1 comment

Comments

@haenf
Copy link

haenf commented Aug 18, 2023

I have the following code:

    lanprops = []
    for lan in languages
        # NOTE: OrderedDict gives error in Mongoc.BSON()
        prop = OrderedDict{String, Any}()
        prop["hasPropertyName"] = "hasLabel"
        prop["hasDataType"] = "string"
        prop["hasSpecifier"] = "language: " * lan
        push!(lanprops, prop)
    end

    initdoc = Dict{String, Any}()
    initdoc["_id"] = "itemType"
    initdoc["hasBaseType"] = "systemItem"
    initdoc["hasProperties"] = lanprops

    Mongoc.transaction(client) do session
        new_item = Mongoc.BSON(initdoc)            
        push!(collection, new_item)
    end

The OrderedDict gives the following error:

Error: ERROR:
│ exception =
│ MethodError: no method matching setindex!(::Mongoc.BSON, ::OrderedCollections.OrderedDict{String, Any}, ::String)

When I change the OrderedDict to a ‘regular’ Dict everything works fine. What can I do to make the OrderedDict working okay in Mongoc.BSON()?

The end result must look like this (the order of the key-value pairs under hasProperties is important):

{
  "_id" : "itemType",
  "hasBaseType" : "systemItem",
  "hasProperties" : [{
      "hasPropertyName" : "hasLabel",
      "hasDataType" : "string",
      "hasSpecifier" : "language: nl"
    }, {
      "hasPropertyName" : "hasLabel",
      "hasDataType" : "string",
      "hasSpecifier" : "language: en"
    }]
}
@haenf
Copy link
Author

haenf commented Aug 19, 2023

I found a very simple solution that gives the same effect as an OrderedDict:

    lanprops = []
    for lan in languages
        prop = Mongoc.BSON()
        prop["hasPropertyName"] = "hasLabel"
        prop["hasDataType"] = "string"
        prop["hasSpecifier"] = "language: " * lan
        push!(lanprops, prop)
    end

    initdoc = Mongoc.BSON()
    initdoc["_id"] = "itemType"
    initdoc["hasBaseType"] = "systemItem"
    initdoc["hasProperties"] = lanprops            
    
    push!(collection, initdoc)

This way the given order of the key-value pair is maintained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant