make mongoDB 用字符串替换单值数组

make mongoDB replace single value array with string

我是 mongoDB 的新手,正在尝试将对象插入数据库。 (哇,比 mySQL... 有趣多了。我正在使用 strongloop 的环回框架及其 mongoDB 连接器。

该对象是我收到的 xml2js 解析的 xml 消息,在解析并插入到 mongo 后,它看起来像这样:

{
    "_id": ObjectID("55c61ee9391da88435c5753f"),
    "offerChange": [
        {
            "foo": [
                "bar"
            ],
            "baz": [
                "foo"
            ]
        }
    ] 
}

如您所见,所有键的值都是数组,尽管它们都只包含一个值。显然,我可以在插入之前或 xml 解析期间将它们转换为字符串,但这需要遍历所有对象的键或首先为工作人员做更多工作,我想避免这种情况。实物比上图大得多。

有没有办法告诉 mongoDB 在创建文档之前或之后自动将只有一个值的数组转换为字符串?

您无需在 application/database 中循环遍历并将数组转换为字符串。 xml2js 为 return 字符串提供一种简洁的方式(如果只有一个项目)和数组(如果有更多项目)。

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

如下启动解析器

parseString(xml, {explicitArray: false}, function (err, result) {
});