如何一次获得多个字段?

时间:2018-01-25 07:12:30

标签: mongodb mongodb-query

我一直在输入此查询

db.sInsert.find(
    {post_id:"28011986676_10155780942281677"},
    {comments_data: 1, _id:0}
)

我从MongoDB得到了这个结果。

{
  "comments_data": {
    "id": "28011986676677",
    "comments": {
      "paging": {
        "cursors": {
          "after": "WTI5dGJXVnVkRjlqZAFhKePQT09",
          "before": "WTI5dGJXVnVkRjlqZAFhKEWXlPQT09"
        }
      },
      "data": [
        {
          "created_time": "2018-01-03T21:23:47+0000",
          "message": "Poor customer care service after became the  customer.I did my  re contract they send acknowledgement email confirmation after no followup.I called again and remains no proper response and action extremely worst customer care service.",
          "from": {
            "name": "Sun",
            "id": "102023391"
          },
          "id": "10155784116677"
        }
      ]
    }
  }
}

如何仅从post_id获取messagecomments_data? 我希望按post_id

进行分组

以下是我的mongoDB中文档的样子

{
        "_id" : ObjectId("5a43aa19d4b45e362428e2da"),
        "comments_data" : {
                "id" : "28011986676_10155780942281677",
                "comments" : {
                        "paging" : {
                                "cursors" : {
                                        "after" : "WTI5dGJXVnVkRjlqZAFhKemIzSTZAN4TlRBeE5EWXlPQT09",
                                        "before" : "WTI5dGJXVnVk4TVRZAMk56YzZANVFV4TlRBeE5EWXlPQT09"
                                }
                        },
                        "data" : [
                                {
                                        "created_time" : "2018-01-03T21:23:47+0000",
                                        "message" : "Poor customer care service after became the Singtel customer.I did my          re contract they send acknowledgement email confirmation after no followup.I called again and remains no proper response and action extremely worst customer care service.",
                                        "from" : {
                                                "name" : "Sundararaju G",
                                                "id" : "1020391"
                                        },
                                        "id" : "10155780942281677_10155811924116677"
                                }
                        ]
                }
        },
        "post_id" : "28011986676_10155780942281677",
        "post_message" : "\"Singtel TV celebrated our 10th birthday with 10 awesome experiences for our customers! Each of our winners won a trip of a lifetime - from attending the Emmy Awards, getting a magical princess treatment at Disneyland, to catching a Premier League game live in London! We thank all our customers for your support and we look forward to more great years to come!\"",
        "reactions_data" : {
                "reactions" : {
                        "paging" : {
                                "cursors" : {
                                        "after" : "TVRBd01EQXpNVEF5T1Rje4TXc9PQZDZD",
                                        "before" : "TVRjNE56TTBBek56a3hNek14TWc9PQZDZD"
                                },
                                "next" : "https://graph.facebook.com/v2.7/280119866761677/reactions?access_token=EAA"
                        },
                        "data" : [
                                {
                                        "type" : "ANGRY",
                                        "id" : "1020573391",
                                        "name" : "Sundararaju Gh"
                                },
                                {
                                        "type" : "LIKE",
                                        "id" : "64721496",
                                        "name" : "Zhiang Xian"
                                }
                        ]
                },
                "id" : "28011986676_102281677"
        }
}

另一个样本

    { "_id" : ObjectId("5a43aa19d4b45e362428e2ee"), "comments_data" : { "id" : "28011986676_10155778255601677",

 "comments" : { "paging" : { "cursors" : { "after" : "WTI5dGJXV5zZANVFV4TXpjM09UTTVNUT09" } }, 
"data" : [ { "created_time" : "2017-12-20T14:16:31+0000", "message" : "Putri Thang Michael Herman hahaha maret jg", "from" : { "name" : "Caterine Liang", "id" : "10153088743299160" }, "id" : "10155778255601677_101171677" }, { "created_time" : "2017-12-20T16:38:12+0000", "message" : "Pauline Chan", "from" : { "name" : "Nitin Aggarwal", "id" : "10153185412203152" }, "id" : "10155778255601677_10155779456916677" } ] } }, 

"post_id" : "28011986676_10155778255601677",
 "post_message" : "\"Peppa Pig and friends return to Singapore in a new musical LIVE show that\\u2019s bigger than ever with life-sized mascots. Singtel mobile subscribers enjoy exclusive priority sale and 15% off tickets to PEPPA & FRIENDS LIVE on Stage from 20 Dec \\u201817. Get your tickets at  now. T&Cs apply.\"",

 "reactions_data" : { "reactions" : { "paging" : { "cursors" : { "after" : "TVRBd01EQXpNREkwTVRc9PQZDZD", "before" : "TVRBd0c9PQZDZD" }, "next" : "https://graph.facebook.com/v2.7/28011986676_1601677/reactions?access_token=EAA" }, "data" : [ { "type" : "LIKE", "id" : "1263712690368750", "name" : "Alice Lo" }, { "type" : "LIKE", "id" : "130228537768890", "name" : "Gina Sangoy" }, { "type" : "LIKE", "id" : "478172222283410", "name" : "Jiamin Feng" }, { "type" : "LIKE", "id" : "10152186445293439", "name" : "Ann Wong" }, { "type" : "LIKE", "id" : "164905927597888", "name" : "ស្រីនាថ ពោធិ៍សាត់" }, { "type" : "LIKE", "id" : "694456787260426", "name" : "Lim Kian Heng" }, { "type" : "LIKE", "id" : "135201790538289", "name" : "Anasta Clara" }, { "type" : "LIKE", "id" : "10154040401150853", "name" : "Jacqueline Salim JingYuan" }, { "type" : "LIKE", "id" : "1428497527466013", "name" : "Izhan Sawalha" }, { "type" : "LIKE", "id" : "10152590923001843", "name" : "Ellena Liu" }, { "type" : "LIKE", "id" : "28011986676_1077" } }

1 个答案:

答案 0 :(得分:1)

这个命令......

db.sInsert.find(
    {post_id:"28011986676_10155780942281677"},
    {'comments_data.comments.data.message': 1, _id:0}
)

...将返回:

{
    "comments_data" : {
        "comments" : {
            "data" : [ 
                {
                    "message" : "Poor customer care service after became the  customer.I did my  re contract they send acknowledgement email confirmation after no followup.I called again and remains no proper response and action extremely worst customer care service."
                }
            ]
        }
    }
}

在您提供的文档中,消息的路径为:comments_data.comments.data.message,因此这是在find()方法的项目参数中使用的正确路径。

如果所需的输出只是 消息,那么在消除响应以消除中间级别的意义上,则此命令...

db.sInsert.aggregate([
  { $match: {post_id: {"$eq": "28011986676_10155780942281677" } } },
  { $project: {'message': '$comments_data.comments.data.message', _id:0} },
])

...将读取相同的数据,但会将输出重新整理为:

{
    "message" : [ 
        "Poor customer care service after became the  customer.I did my  re contract they send acknowledgement email confirmation after no followup.I called again and remains no proper response and action extremely worst customer care service."
    ]
}