2

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?

5
  • That is not a wise idea. In terms of "object consistency" it is much better to leave these as arrays even if they only contain a single entry. Why? Think of defining an object in code. What "type" to you give to the field. "Array or maybe String?". Does not make a lot on sense for validity checking now does it? Whilst MongoDB documents are schemaless and there is nothing stopping you from verying the structure between documents, it does not been you should ( in a way other than a clear "inheritance pattern" ). So it would be "advised" to really keep it that way. Commented Aug 8, 2015 at 16:13
  • That said, if your only problem is the rather "quirky" way that XML translators deal with things you only ever want as a singular value, then by all means make that clear in your question with the specific code you are currently using, and I or someone else will suggest a "fix" to that so yo get a singular value every time. Commented Aug 8, 2015 at 16:15
  • @BlakesSeven no, actually I think your first comment is very reasonable and I think I'll just keep it. Commented Aug 8, 2015 at 16:18
  • 1
    The main point I am making there is suppose it is just { "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 $push operation to the already present array. Which yeilds { "foo": ["bar","baz"] }. That is what I wanted you to consider, and I hope that you have. Commented Aug 8, 2015 at 16:22
  • 1
    +1 to Blake Seven's answer. If you really need the value as a string, convert it when you need it to keep your CRUD operations consistent. Commented Aug 8, 2015 at 21:34

1 Answer 1

1

You need not loop through and convert from array to string in application/database. xml2js provide a neat way to return string if there is only one item and array if there are more items.

explicitArray (default: true): Always put child nodes in an array if true; otherwise an array is created only if there is more than one.

Initiate your parser as follows

parseString(xml, {explicitArray: false}, function (err, result) {
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.