我有以下收藏品:
> db.website.find()
{ "_id" : 1, "domainName" : "test1.com", "hosting" : "hostgator.com" }
{ "_id" : 2, "domainName" : "test2.com", "hosting" : "aws.amazon.com" }
{ "_id" : 3, "domainName" : "test3.com", "hosting" : "aws.amazon.com" }
{ "_id" : 4, "domainName" : "test4.com", "hosting" : "hostgator.com" }
{ "_id" : 5, "domainName" : "test5.com", "hosting" : "aws.amazon.com" }
{ "_id" : 6, "domainName" : "test6.com", "hosting" : "cloud.google.com" }
{ "_id" : 7, "domainName" : "test7.com", "hosting" : "aws.amazon.com" }
{ "_id" : 8, "domainName" : "test8.com", "hosting" : "hostgator.com" }
{ "_id" : 9, "domainName" : "test9.com", "hosting" : "cloud.google.com" }
{ "_id" : 10, "domainName" : "test10.com", "hosting" : "godaddy.com" }
和这一个:
db.websitegroup.find()
{ "_id" : "aws.amazon.com", "total" : 4, "hosting" : "aws.amazon.com" }
{ "_id" : "hostgator.com", "total" : 3, "hosting" : "hostgator.com" }
{ "_id" : "cloud.google.com", "total" : 2, "hosting" : "cloud.google.com" }
{ "_id" : "godaddy.com", "total" : 1, "hosting" : "godaddy.com" }
我想要一个.csv报告,其中包含网站集的所有字段+网站组的总字段,所以我在mongo控制台中执行了以下操作:
var joinTable = db.website.aggregate([{$lookup:{from:"websitegroup",localField:"hosting",foreignField:"hosting",as:"datosadjuntos"}}]);
db.createCollection("joinTable");
db.joinTable.insert(joinTable.toArray());
在普通控制台中我使用了mongoexport:
mongoexport --db test --collection joinTable --type=csv --out /home/joinTable_`date +"%d-%m-%y-%H:%M:%S"`.csv --fields _id,domainName,hosting,datosadjuntos.total
除了datosadjuntos.total
之外,我得到了所有字段如果我再次导出但是替换datosadjuntos.total只是为了datosadjuntos.total:
mongoexport --db test --collection joinTable --type=csv --out /home/lavenec1/reportes_soyplus/joinTable_`date +"%d-%m-%y-%H:%M:%S"`.csv --fields _id,domainName,hosting,datosadjuntos
然后我明白了:
似乎每个记录周围都有一个[]
如何正确使用datosadjuntos.total获取此报告?
答案 0 :(得分:2)
不可能只导出" datosadjuntos"的子字段。根据{{3}}文档:
对于JSON输出格式,mongoexport仅包含指定的字段和_id字段,如果指定的字段是子文档中的字段,则mongoexport包含具有其所有字段的子文档,而不仅仅是文档中的指定字段。
尝试修改聚合以便以mongoexport可访问的方式返回数据。
mongoexport返回数组字段,因为每个查找值可能会返回多个值。您希望在聚合管道的末尾添加$lookup阶段,以允许直接访问每个结果文档,并将结果$unwind发送到导出可以访问的字段。
例如:
db.website.aggregate([
{$lookup:{from:"websitegroup",localField:"hosting",foreignField:"hosting",as:"datosadjuntos"}},
{ "$unwind" : { "path" : "$datosadjuntos"}},
{ "$project" : { "_id" : 1, "domainName" : 1, "hosting" : 1, "datos_total" : "$datosadjuntos.total" } }
]);
结果如下:
{ "_id" : 2, "domainName" : "test2.com", "hosting" : "aws.amazon.com", "datos_total" : 4 }
{ "_id" : 3, "domainName" : "test3.com", "hosting" : "aws.amazon.com", "datos_total" : 4 }
{ "_id" : 4, "domainName" : "test4.com", "hosting" : "hostgator.com", "datos_total" : 3 }
{ "_id" : 5, "domainName" : "test5.com", "hosting" : "aws.amazon.com", "datos_total" : 4 }
{ "_id" : 6, "domainName" : "test6.com", "hosting" : "cloud.google.com", "datos_total" : 2 }
{ "_id" : 7, "domainName" : "test7.com", "hosting" : "aws.amazon.com", "datos_total" : 4 }
{ "_id" : 8, "domainName" : "test8.com", "hosting" : "hostgator.com", "datos_total" : 3 }
{ "_id" : 9, "domainName" : "test9.com", "hosting" : "cloud.google.com", "datos_total" : 2 }
{ "_id" : 10, "domainName" : "test10.com", "hosting" : "godaddy.com", "datos_total" : 1 }