MongoDB中的JSON结构数据操作



我是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钻插入数据,但在此之前,我想在适当的层次结构的数据结构。提前感谢!

使用mongo聚合框架获取结果

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()

如果您想将上述结果添加到新集合中,则在aggregation中添加$out

相关内容

  • 没有找到相关文章

最新更新