I'm very new to mongoDB and am trying to insert an object to the database. (Wow, much more fun than mySQL...). I'm using strongloop's loopback framework and its mongoDB connector.
The object is a xml2js parsed xml message I receive, after parsing and inserting to mongo it looks like this:
{
"_id": ObjectID("55c61ee9391da88435c5753f"),
"offerChange": [
{
"foo": [
"bar"
],
"baz": [
"foo"
]
}
]
}
as you can see, all the key's values are arrays, though all of them contain only one value. Obviously I can convert them to strings before the insert or during xml parsing, but that would require looping over all the object's keys or more work for the worker in the first place, which I'd like to avoid. The real object is much bigger than above shown.
Is there a way to tell mongoDB to automatically convert arrays that have only one value to a string before or after the document gets created?
{ "foo": "bar" }instead of an array. So then what happens if you want to assign "baz" to the "foo" property in addition to "bar"? This blows up the logic and simple updates. But with{ "foo": ["bar"] }in place the update is a simple$pushoperation to the already present array. Which yeilds{ "foo": ["bar","baz"] }. That is what I wanted you to consider, and I hope that you have.