MongoDB子字段的列表投影 [英] MongoDB list projection of subfield

查看:202
本文介绍了MongoDB子字段的列表投影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与SQL类似,我需要SELECT root.subfield1.subfield2 FROM collection,它产生 subfield2的列表 ... 使用示例:通过

With SQL analogy, I need to SELECT root.subfield1.subfield2 FROM collection, that produces a list of subfield2 ... Using a Example: import this datapackage.json by

mongoimport -d lang_db -c lang_meta datapackage.json --jsonArray

并使用mongo命令在终端上工作:

and work at terminal with mongo command:

db.lang_meta.find({},{"resources.schema.fields.name":2})

db.lang_meta.find({},{"resources.schema.fields.name":2})

结果是一个数组元素(.count() = 1),其子字段包含名称,而不是名称列表.

the result is one array element (.count()=1) with subfields containing the names, not a list of names.

获得:

{
    "_id": ObjectId("56011be94564569fc920eda4"),
    "resources": [{
    "schema": {
        "fields": [{
            "name": "alpha2"
        }, {
            "name": "English"
        }]
    }
    }, {
    "schema": {
        "fields": [{
            "name": "alpha3-b"
        }, {
            "name": "alpha2"
        }, {
            "name": "English"
        }]
    }
    }, {
    "schema": {
        "fields": [{
            "name": "alpha3-b"
        }, {
            "name": "alpha3-t"
        }, {
            "name": "alpha2"
        }, {
            "name": "English"
        }, {
            "name": "French"
        }]
    }
    }, {
    "schema": {
        "fields": [{
            "name": "lang"
        }, {
            "name": "langType"
        }, {
            "name": "territory"
        }, {
            "name": "revGenDate"
        }, {
            "name": "defs"
        }, {
            "name": "dftLang"
        }, {
            "name": "file"
        }]
    }
    }]
}

想要:

"alpha2",英语","alpha3-b","alpha2",英语" ...

"alpha2","English","alpha3-b", "alpha2", "English" ...

推荐答案

我找到了命令!不是find():-)

I find the command!! it is not find() :-)

db.lang_meta.distinct("resources.schema.fields.name")

db.collection.distinct

(编辑)

糟糕,严格正确"的答案是列表(可能重复出现),而不是 set (没有重复发生).请参阅db.lang_meta.distinct("resources.mediatype")的情况,其中正确的解决方案必须返回四个重复项的列表,而不仅仅是四个.

Ops, the "strictly correct" answer is a list (where repeat itens is possible) not a set (where no repeat occurs). See the case of db.lang_meta.distinct("resources.mediatype"), where the correct solution must return a list of four repeated items, not only one.

对于 list ,我们可以使用map() ...嗯,假设只有一项,那就是...

For list we can use map()... Well, suppose only one item, it would be ...

db.lang_meta.find().map(function(c) { 
  return c.resources[0].schema.fields[0].name; 
});

,但是必须遍历.resources.fields,所以

but must iterate over .resources and over .fields, so

db.lang_meta.find().map(function(c) {
    var ret = [];
    for (var i=0; i<c.resources.length; i++) 
        for (var j=0; j<c.resources[i].schema.fields.length; j++) 
             ret.push( c.resources[i].schema.fields[j].name );
   return ret;
});

...接近但不是理想(优雅)的解决方案.

... that is near but not the ideal (elegant) solution.

回到resources.mediatype示例,它是对"repeat itens"的更好说明,

Returning to the resources.mediatype example, that is a better illustration to the "repeat itens",

db.lang_meta.find().map(function(c) {
    var ret = [];
    for (var i=0; i<c.resources.length; i++) 
             ret.push( c.resources[i].mediatype );
   return ret;
});

产生"text/csv", "text/csv", "text/csv", "text/csv"(!) 但采用的是数组结构...而不是简单的数组.

That produces "text/csv", "text/csv", "text/csv", "text/csv" (!) but in a array-of-array structure... not a simple array.

让我们用db.lang_meta.find({},{"resources.schema.fields.name":1})做某事...

Lets do something with db.lang_meta.find({},{"resources.schema.fields.name":1}) ...

这篇关于MongoDB子字段的列表投影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆