简体   繁体   中英

Mongoose mixed type schema is getting processed as an array of keys of type String, but no values

I have a mongoose schema that looks something like this:

"datetime": {"type": Date}
"sequence": {"type": Number}
"extra": {"type": mongoose.Schema.Types.Mixed}

For some reason, the "extra" key in my model is processing dictionary's of key-value pairs as an array of strings, where each string is a key in the dictionary. For example, the following data:

{
    "datetime": new Date(),
    "sequence": 10,
    "extra": {"val1": 5, "val2": "hello"}
}

is getting processed as follows:

{
    "datetime":  [Date object]
    "sequence": 10,
    "extra": ["val1", "val2"]
}

This data is being populated in Python, and the "extra" field is being provided as a Python dictionary. The entire data set is then forwarded to a REST API via the requests module. The contents of this "extra" field can vary quite considerably, which is why I had it defined as "mixed" in my model.

Does anyone know why mongoose is parsing this field this way?

I think my problem was related to the different between request 's optional data / json parameters. I was using:

request.post(url, myDictionary)

which defaults to the data key, when I should have been using:

request.post(url, json=myDictionary)

The problem with this, of course, is that a Python datetime object can not be serialized here, so I simply used time.time() instead.

In regards to my mongoose schema, the "extra" field doesn't have a defined schema, so that data is simply provided "as-is".

Hopefully this is useful in the event that someone has a similar problem at some point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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