我正在尝试从MongoDB的数据集中获取竞争者的层次结构,我的代码已尽我所能,但由于文档的大小,输出难以阅读。
我已经研究了$ project,并试图以这种方式最小化查找的大小,但无法弄清楚。
db.companies.aggregate([
{$match: {tag_list: { $regex: /computer/, $options: 'i' }, competitions:{$ne:[]}}},
{ $graphLookup: { from: "companies", startWith:
"$competitions.competitor.name", connectFromField:"competitions.competitor.name",
connectToField: "name", as: "competition_heirachy"}},
{$limit:20},
{$project: {_id:0, name:1, tag_list:1, competitions:1, competition_heirachy:1}}
]).pretty()
输出:
{
"name" : "Prevail Resources",
"tag_list" : "field-services, break-fix-services, technical-fix, computer-repair, desktop-support, it-staffing, it-consulting, dell-repair, ibm-repair, sony-repair, lexmark-repair, pc-repair, laptop-repair, notebook-repair, printer-repair",
"competitions" : [
{
"competitor" : {
"name" : "Modis IT",
"permalink" : "modis-it"
}
},
{
"competitor" : {
"name" : "Matrix Resources",
"permalink" : "matrix-resources"
}
},
{
"competitor" : {
"name" : "Innostaff",
"permalink" : "innostaff"
}
}
],
"competition_heirachy" : [
{
"_id" : ObjectId("52cdef7e4bab8bd67529bbe0"),
"name" : "Matrix Resources",
"permalink" : "matrix-resources",
"crunchbase_url" : "http://www.crunchbase.com/company/matrix-resources",
"homepage_url" : "http://www.matrixresources.com",
"blog_url" : "",
"blog_feed_url" : "",
"twitter_username" : "matrixresources",
"category_code" : "enterprise",
"number_of_employees" : null,
"founded_year" : 1983,
"founded_month" : null,
"founded_day" : null,
"deadpooled_year" : null,
"deadpooled_month" : null,
"deadpooled_day" : null,
"deadpooled_url" : null,
"tag_list" : null
... and the rest of the "Matrix Resources" document continues
我只需要显示查询的$ project列表中的字段,而不是整个文档。