如何从MongoDB结果中删除额外的json密钥

时间:2016-04-02 07:13:04

标签: json mongodb restheart cjson

我刚开始使用MongoDB和RESTHeart api服务器。

我希望用MongoDB替换现有的数据库,现有的DB也从其余的api接收json,我认为改变很少。所以,下面是问题:

当我查询MongoDB时,这就是我得到的

{
    "_collection-props-cached": false,
    "_embedded": {
        "rh:doc": [
            {
                "_etag": {
                    "$oid": "56ff559b7ea5c11edc8f93b7"
                },
                "_id": {
                    "$oid": "56ff559b7ea5c11edc8f93b6"
                },
                "_links": {
                    "self": {
                        "href": "/presence/active_watchers/56ff559b7ea5c11edc8f93b6"
                    }
                },
                "callid": "1-12285@10.0.1.168",
                "contact": "sip:service@10.0.1.168:5060;transport=UDP",
                "event": "presence",
            },
            {
                "_etag": {
                    "$oid": "56ff55897ea5c11edc8f93b5"
                },
                "_id": {
                    "$oid": "56ff55897ea5c11edc8f93b4"
                },
                "_links": {
                    "self": {
                        "href": "/presence/active_watchers/56ff55897ea5c11edc8f93b4"
                    }
                },
                "callid": "1-12285@10.0.1.168",
                "contact": "sip:service@10.0.1.168:5060;transport=UDP",
                "event": "presence",
                "event_id": "",
            }
        ]
    },
    "_etag": {
        "$oid": "56ff44807ea5c11edc8f93a6"
    },
    "_id": "active_watchers",
    "_links": {
        "curies": [],
        "self": {
            "href": "/presence/active_watchers"
        }
    },
    "_returned": 2,
    "descr": "subscriptions collection"
}

我只对 rh_doc 数组感兴趣。我的问题是,有没有办法只收到Monogo的文件而没有额外的信息。

问题是,现有代码只需要像[{callid:"123",...},{callid:"234",...}]这样的值数组,并且使用cJSON在C中编码。触摸C代码感觉很可怕!!

或者可能是CJSON可以删除密钥: _etag,_id 等。

修改 这是我查询的方式:

http  GET "http://localhost:8080/presence/active_watchers?filter={'presentity_uri':'sip:service-1@opensipstest.org'}&filter={'event':'presence'}"

由于

1 个答案:

答案 0 :(得分:0)

在find语句的第二个参数中,您可以指定要包含的字段。

E.g。

db.someCollection.find({}, {_embedded: 1})

或者,如果您只想要嵌套的rh:docs字段:

db.someCollection.find({}, {"_embedded.rh:doc": 1})
相关问题