JSON MongoDB 中的结构数据操作

JSON Structure Data Manipulation in MongoDB

我是 mongodb 的新手,遇到了数据结构问题。数据的层次结构不可见。例如,我的数据格式为

{
        "FCILTY_ID" : 154,
        "ACCT_NO" : 2.14782e+008,
        "STRING_DC_CD" : 8,
        "STRING_DTS" : "25-JAN-14",
        "STRING_ID_NO" : 1,
        "STRING_ITEM_NO" : 0,
        "CHILD_OF_CD" : "",
        "BINTYPE_NO" : 244,
        "PTXT_CODE_STR" : "8.1.71.4.0.0.0.13",
        "PTXT_DESC_TXT" : "DC DATE =",
        "VALUE_NO" : 2.37024e+007,
        "VALUE_FREETEXT_TXT" : "",
        "VALUE_DTS" : "25-JAN-14" 
}
 {
     "FCILTY_ID" : 154,
    "ACCT_NO" : 2.14782e+008,
    "STRING_DC_CD" : 8,
    "STRING_DTS" : "25-JAN-14",
    "STRING_ID_NO" : 1,
    "STRING_ITEM_NO" : 2,
    "CHILD_OF_CD" : "",
    "BINTYPE_NO" : 244,
    "PTXT_CODE_STR" : "8.1.71.4.0.0.0.167",
    "PTXT_DESC_TXT" : "START TIME",
    "VALUE_NO" : 2.37024e+007,
    "VALUE_FREETEXT_TXT" : "",
    "VALUE_DTS" : "25-JAN-14"
}
 {
    "FCILTY_ID" : 154,
    "ACCT_NO" : 2.14782e+008,
    "STRING_DC_CD" : 8,
    "STRING_DTS" : "25-JAN-14",
    "STRING_ID_NO" : 1,
    "STRING_ITEM_NO" : 3,
    "CHILD_OF_CD" : "",
    "BINTYPE_NO" : 241,
    "PTXT_CODE_STR" : "8.1.71.4.0.0.0.153",
    "PTXT_DESC_TXT" : "ORDER TYPE",
    "VALUE_NO" : 0,
    "VALUE_FREETEXT_TXT" : "",
    "VALUE_DTS" : ""
}

我正在努力但无法使用聚合和更新使数据具有理想的结构。我想要这样的数据结构

    {
     "FCILTY_ID" : 154,
        "ACCT_NO" : 2.14782e+008,
        "STRING_DC_CD" : 8,
        "STRING_DTS" : "25-JAN-14",
        "STRING_ID_NO" : 1,

        "ITEM":
[ {

    {"STRING_ITEM_NO" : 0,
        "CHILD_OF_CD" : "",
        "BINTYPE_NO" : 244,
        "PTXT_CODE_STR" : "8.1.71.4.0.0.0.13",
        "PTXT_DESC_TXT" : "DC DATE =",
        "VALUE_NO" : 2.37024e+007,
        "VALUE_FREETEXT_TXT" : "",
        "VALUE_DTS" : "25-JAN-14"}

    {
    "STRING_ITEM_NO" : 2,
        "CHILD_OF_CD" : "",
        "BINTYPE_NO" : 244,
        "PTXT_CODE_STR" : "8.1.71.4.0.0.0.167",
        "PTXT_DESC_TXT" : "START TIME",
        "VALUE_NO" : 2.37024e+007,
        "VALUE_FREETEXT_TXT" : "",
        "VALUE_DTS" : "25-JAN-14"}

    {
    "STRING_ITEM_NO" : 3,
        "CHILD_OF_CD" : "",
        "BINTYPE_NO" : 241,
        "PTXT_CODE_STR" : "8.1.71.4.0.0.0.153",
        "PTXT_DESC_TXT" : "ORDER TYPE",
        "VALUE_NO" : 0,
        "VALUE_FREETEXT_TXT" : "",
        "VALUE_DTS" : ""}
    }

]    }

这在 mongodb 中可行吗?如果没有,我可以使用任何其他工具来实现这一目标。我想在 apache drill 中插入数据,但在此之前我希望数据结构处于适当的层次结构中。提前致谢!

使用mongo aggregation framework得到结果。

1> 第一组 FCILTY_ID,ACCT_NO,STRING_DC_CD,STRING_DTS,STRING_ID_NO

2> 将所有剩余数据推送到 ITEM

3> 最后使用的项目。

所以 mongo 聚合查询看起来像:

db.collectionName.aggregate([
    {
        "$group": {
            "_id": {
                "FCILTY_ID": "$FCILTY_ID",
                "ACCT_NO": "$ACCT_NO",
                "STRING_DC_CD": "$STRING_DC_CD",
                "STRING_DTS": "$STRING_DTS",
                "STRING_ID_NO": "$STRING_ID_NO"
            },
            "ITEM": {
                "$push": {
                    "STRING_ITEM_NO": "$STRING_ITEM_NO",
                    "CHILD_OF_CD": "$CHILD_OF_CD",
                    "BINTYPE_NO": "$BINTYPE_NO",
                    "PTXT_CODE_STR": "$PTXT_CODE_STR",
                    "PTXT_DESC_TXT": "$PTXT_DESC_TXT",
                    "VALUE_NO": "$VALUE_NO",
                    "VALUE_FREETEXT_TXT": "$VALUE_FREETEXT_TXT",
                    "VALUE_DTS": "$VALUE_DTS"
                }
            }
        }
    },
    {
        "$project": {
            "_id": 0,
            "FCILTY_ID": "$_id.FCILTY_ID",
            "ACCT_NO": "$_id.ACCT_NO",
            "STRING_DC_CD": "$_id.STRING_DC_CD",
            "STRING_DTS": "$_id.STRING_DTS",
            "STRING_ID_NO": "$_id.sid",
            "ITEM": 1
        }
    }
]).pretty()

如果要在新集合中添加上述结果,请在聚合中添加$out