当我试图从MongoDB获取数据时。我可以使用此查询显示我想要实现的目标
db.sInsert.distinct(
"comments_data.comments.data.message",
{post_id: {"$eq": "28011986676" }}
)
结果是
[ "Who else loves Apple ?" ]
另外,我的文档看起来
{
"_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"
}
}
但当我试图把它输出时,下面这句话。我遇到错误
mongoexport --db sDB --collection sInsert --query '{"post_id":{$gte:28011986676}}',{ comments_data.comments.data.message}' --out test.json
错误信息的位置
2018-01-29T19:56:31.442+0800 too many positional arguments: [comments_data.comments.data.message}']
2018-01-29T19:56:31.526+0800 try 'mongoexport --help' for more information
我可以问一下,有没有可能导出我想要实现的东西,比如如何在MongoDB中显示出来?
最佳答案
MongoExport命令的这一部分…
--query '{"post_id":{$gte:28011986676}}',{ comments_data.comments.data.message}'
…建议您尝试使用
query
参数指定过滤器和投影。根据the docs的query
参数:提供一个json文档作为查询,可以选择限制导出中返回的文档。
所以,那只是过滤器的问题。
您可以使用
fields
参数将输出限制为特定字段。从the docs:指定要包含在导出中的一个或多个字段。使用逗号分隔的字段列表指定多个字段。
因此,您的
mongoexport
命令应该是:mongoexport --db sDB --collection sInsert --query '{"post_id":{$gte:28011986676}}' --fields "comments_data.comments.data.message" --out test.json
但是,由于您选择了json输出格式,因此以下(来自文档)是相关的:
对于json输出格式,mongoexport只包括指定字段和id字段,如果指定字段是子文档中的字段,mongoexport则包括子文档及其所有字段,而不仅仅是文档中的指定字段。
由于试图从子文档中选择特定字段并使用json输出格式,
mongoexport
将包括“子文档及其所有字段,而不仅仅是文档中的指定字段”。这听起来很像earlier question from you中提出的问题。
关于mongodb - MongoExport使用查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48500880/