Mongodb 如何一次获取多个字段?

Mongodb 如何一次获取多个字段?,mongodb,mongodb-query,Mongodb,Mongodb Query,我一直在输入这个查询 db.sInsert.find( {post_id:"28011986676_10155780942281677"}, {comments_data: 1, _id:0} ) 我从MongoDB得到了这个结果 { "comments_data": { "id": "28011986676677", "comments": { "paging": { "cursors": { "after"

我一直在输入这个查询

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"
        }
      ]
    }
  }
}
如何仅从
comments\u data
获取
post\u id
消息
? 我想按
post\u 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" } }
这个命令

db.sInsert.find(
    {post_id:"28011986676_10155780942281677"},
    {'comments_data.comments.data.message': 1, _id:0}
)
db.sInsert.aggregate([
  { $match: {post_id: {"$eq": "28011986676_10155780942281677" } } },
  { $project: {'message': '$comments_data.comments.data.message', _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\u data.comments.data.message
,因此这是在
find()
方法的projeciton参数中使用的正确路径

如果所需的输出只是一条消息,在将响应平坦化以删除中间级别的意义上,则此命令

db.sInsert.find(
    {post_id:"28011986676_10155780942281677"},
    {'comments_data.comments.data.message': 1, _id:0}
)
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."
    ]
} 

你好它只显示“消息”输出。但是为什么我有重复的结果?你能解释一下“我有重复的结果”是什么意思吗?你原来的问题中没有关于重复结果的内容。对不起。因为当我运行您提供的命令时,我有20条消息“客户服务差等”的结果,但实际上我在评论数据中仍有其他消息在相同的帖子id下,但它没有打印出来,这表明
db.sInsert
中有20个文档具有
post\u id="28011986676_10155780942281677“
。如果重新运行该命令,但从
$project
子句中删除
\u id:0
,您将看到20个单独的文档,每个文档都有不同的
\u id
$match
子句控制选择哪些文档,
$project
子句控制从每个文档中选择哪些数据。
$project
子句成功地将输出限制为
消息
,如果要将结果限制为单个文档,则需要创建一个与单个文档匹配的
$match
子句。谢谢!所以我必须这样做?聚合([{$match:{post_id:{“$eq”:“28011986676_101557809422281677”}}},{$match:{'message':'$comments_data.comments.data.message',_id:0}},]