如何从mongoDB获取数据,如HashMap(键值对)

时间:2018-02-02 08:09:19

标签: mongodb hashmap mongodb-query aggregation-framework

以下是我的mongodb中的记录

var test = function test() {
   return React.createElement(
      "div",
      null,
      "this is ",
      React.createElement(
         "b",
         null,
         "a test"
      ),
      " text"
   );
};

我希望将其作为键值对:  键应为“properties.device_id”并为整个记录值

喜欢这个

{
    "_id": "5a65a047992e3c2572f74102",
    "_class": "com.vuelogix.location.model.LocationModel",
    "type": "Feature",
    "properties": {
        "address": "Purna to Loha Rd, Maharashtra 431511, India",
        "device_id": 23613,
        "last_updated": "2018-01-22T08:26:47.237Z"
    },
    "geometry": {
        "_class": "com.vuelogix.location.model.geojson.geometry.Point",
        "coordinates": [77.065659, 19.145168],
        "type": "Point"
    }
},

{
    "_id": "5a65ae1e992e3c2572f74114",
    "_class": "com.vuelogix.location.model.LocationModel",
    "type": "Feature",
    "properties": {
        "address": "Taranagar - Churu Rd, Chalkoi Baneerotan, Rajasthan 331001, India",
        "device_id": 23658,
        "last_updated": "2018-01-22T09:25:50.893Z"
    },
    "geometry": {
        "_class": "com.vuelogix.location.model.geojson.geometry.Point",
        "coordinates": [74.956284, 28.497661],
        "type": "Point"
    }
}

有没有办法在没有迭代记录的情况下得到这样的结果?

1 个答案:

答案 0 :(得分:1)

使用 $addFields 管道阶段创建一个新字段root,该字段是包含两个字段k和{{}的文档数组1}}其中:

v

在您的情况下,The k field contains the field name. The v field contains the value of the field. 应为k字段。由于这是一种双重类型,因此您需要将其转换为字符串以供日后使用。因此,您的初始管道如下所示:

device_id

将返回以下文件

db.collection.aggregate([
    {
        "$addFields": {
            "root": [
                {
                    "k": { "$substr": [ "$properties.device_id", 0, -1 ] },
                    "v": "$$ROOT"
                }
            ]                
        }
    }
])

从这里开始,您可以利用 $arrayToObject 运算符,以便将新添加的根转换为以/* 1 */ { "_id" : "5a65a047992e3c2572f74102", "_class" : "com.vuelogix.location.model.LocationModel", "type" : "Feature", "properties" : { "address" : "Purna to Loha Rd, Maharashtra 431511, India", "device_id" : 23613.0, "last_updated" : "2018-01-22T08:26:47.237Z" }, "geometry" : { "_class" : "com.vuelogix.location.model.geojson.geometry.Point", "coordinates" : [ 77.065659, 19.145168 ], "type" : "Point" }, "root" : [ { "k" : "23613", "v" : { "_id" : "5a65a047992e3c2572f74102", "_class" : "com.vuelogix.location.model.LocationModel", "type" : "Feature", "properties" : { "address" : "Purna to Loha Rd, Maharashtra 431511, India", "device_id" : 23613.0, "last_updated" : "2018-01-22T08:26:47.237Z" }, "geometry" : { "_class" : "com.vuelogix.location.model.geojson.geometry.Point", "coordinates" : [ 77.065659, 19.145168 ], "type" : "Point" } } } ] } /* 2 */ { "_id" : "5a65ae1e992e3c2572f74114", "_class" : "com.vuelogix.location.model.LocationModel", "type" : "Feature", "properties" : { "address" : "Taranagar - Churu Rd, Chalkoi Baneerotan, Rajasthan 331001, India", "device_id" : 23658.0, "last_updated" : "2018-01-22T09:25:50.893Z" }, "geometry" : { "_class" : "com.vuelogix.location.model.geojson.geometry.Point", "coordinates" : [ 74.956284, 28.497661 ], "type" : "Point" }, "root" : [ { "k" : "23658", "v" : { "_id" : "5a65ae1e992e3c2572f74114", "_class" : "com.vuelogix.location.model.LocationModel", "type" : "Feature", "properties" : { "address" : "Taranagar - Churu Rd, Chalkoi Baneerotan, Rajasthan 331001, India", "device_id" : 23658.0, "last_updated" : "2018-01-22T09:25:50.893Z" }, "geometry" : { "_class" : "com.vuelogix.location.model.geojson.geometry.Point", "coordinates" : [ 74.956284, 28.497661 ], "type" : "Point" } } } ] } 为关键字的对象:

device_id

输出:

db.collection.aggregate([
    {
        "$addFields": {
            "root": [
                {
                    "k": { "$substr": [ "$properties.device_id", 0, -1 ] },
                    "v": "$$ROOT"
                }
            ]                
        }
    },
    {
        "$addFields": {
            "root": {
                "$arrayToObject": "$root"
            }             
        }
    }
])

管道中的最后一步是使用$replaceRoot管道运算符来获得所需的输出:

/* 1 */
{
    "_id" : "5a65a047992e3c2572f74102",
    "_class" : "com.vuelogix.location.model.LocationModel",
    "type" : "Feature",
    "properties" : {
        "address" : "Purna to Loha Rd, Maharashtra 431511, India",
        "device_id" : 23613.0,
        "last_updated" : "2018-01-22T08:26:47.237Z"
    },
    "geometry" : {
        "_class" : "com.vuelogix.location.model.geojson.geometry.Point",
        "coordinates" : [ 
            77.065659, 
            19.145168
        ],
        "type" : "Point"
    },
    "root" : {
        "23613" : {
            "_id" : "5a65a047992e3c2572f74102",
            "_class" : "com.vuelogix.location.model.LocationModel",
            "type" : "Feature",
            "properties" : {
                "address" : "Purna to Loha Rd, Maharashtra 431511, India",
                "device_id" : 23613.0,
                "last_updated" : "2018-01-22T08:26:47.237Z"
            },
            "geometry" : {
                "_class" : "com.vuelogix.location.model.geojson.geometry.Point",
                "coordinates" : [ 
                    77.065659, 
                    19.145168
                ],
                "type" : "Point"
            }
        }
    }
}

/* 2 */
{
    "_id" : "5a65ae1e992e3c2572f74114",
    "_class" : "com.vuelogix.location.model.LocationModel",
    "type" : "Feature",
    "properties" : {
        "address" : "Taranagar - Churu Rd, Chalkoi Baneerotan, Rajasthan 331001, India",
        "device_id" : 23658.0,
        "last_updated" : "2018-01-22T09:25:50.893Z"
    },
    "geometry" : {
        "_class" : "com.vuelogix.location.model.geojson.geometry.Point",
        "coordinates" : [ 
            74.956284, 
            28.497661
        ],
        "type" : "Point"
    },
    "root" : {
        "23658" : {
            "_id" : "5a65ae1e992e3c2572f74114",
            "_class" : "com.vuelogix.location.model.LocationModel",
            "type" : "Feature",
            "properties" : {
                "address" : "Taranagar - Churu Rd, Chalkoi Baneerotan, Rajasthan 331001, India",
                "device_id" : 23658.0,
                "last_updated" : "2018-01-22T09:25:50.893Z"
            },
            "geometry" : {
                "_class" : "com.vuelogix.location.model.geojson.geometry.Point",
                "coordinates" : [ 
                    74.956284, 
                    28.497661
                ],
                "type" : "Point"
            }
        }
    }
}

<强>输出

db.collection.aggregate([
    {
        "$addFields": {
            "root": [
                {
                    "k": { "$substr": [ "$properties.device_id", 0, -1 ] },
                    "v": "$$ROOT"
                }
            ]                
        }
    },
    {
        "$addFields": {
            "root": {
                "$arrayToObject": "$root"
            }             
        }
    },
    { "$replaceRoot" : { "newRoot": "$root" } }    
])