本文介绍了MongoDB返回没有任何父文档的嵌入式文档的嵌入式文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 MongoDB 中编写了以下代码:

db.departments.insert({name:"Accounting",teams:[{名称:阿尔法团队",员工:[{姓名:约翰"},{名称:大卫"}]},{名称:真正的团队",员工:[{名称:奥利弗"},{名称:山姆"}]}]});db.departments.insert({名称:工程",团队:[{名称:蓝队",员工:[{名称:辛格"},{名称:简"}]},{名称:懒人团队",员工:[{name:"marleen"},{名称:佛罗伦萨"}]}]});

我想做 SELECT name FROM employee WHERE name LIKE '%o%' ORDER BY name DESC 的等价物并得到结果 [{"name":"oliver"},{"name":"john"},{"name":"florence"}].

我刚刚意识到预测,所以我尝试了这个:

db.departments.find({"teams.employees.name":/.*o.*/}, {_id:0, "teams.employees.name": 1}).pretty();

但结果是这样的:

{团队":[{雇员" : [{姓名":约翰"},{姓名":大卫"}]},{雇员" : [{姓名":奥利弗"},{姓名":山姆"}]}]}{团队":[{雇员" : [{姓名":辛格"},{姓名":简"}]},{雇员" : [{姓名":玛琳"},{名称":佛罗伦萨"}]}]}

我的语法有什么问题?我愿意接受其他建议以实现我的目标

解决方案

使用

db.departments.aggregate([{ $unwind: "$teams" },{ $unwind: "$teams.employees" },{ $project: { _id: 0, name: "$teams.employees.name" } },{ $match: { "name": { $regex:/.*o.*/}} }]).toArray()

结果

[{姓名":约翰"},{姓名":奥利弗"},{名称":佛罗伦萨"}]

I wrote the following code in MongoDB:

db.departments.insert({name:"Accounting",teams:[
    {name:"Alpha team",employees:[
        {name:"john"},
        {name:"david"}
    ]},
    {name:"True team",employees:[
        {name:"oliver"},
        {name:"sam"}
    ]}
]});

db.departments.insert({name:"Engineering",teams:[
    {name:"Blue team",employees:[
        {name:"singh"},
        {name:"jane"}
    ]},
    {name:"Lazy team",employees:[
        {name:"marleen"},
        {name:"florence"}
    ]}
]});

I want to do the equivalent of SELECT name FROM employee WHERE name LIKE '%o%' ORDER BY name DESC and get the results [{"name":"oliver"},{"name":"john"},{"name":"florence"}].

I just became aware of projections, so I tried this:

db.departments.find({"teams.employees.name":/.*o.*/}, {_id:0, "teams.employees.name": 1}).pretty();

But the result was this instead:

{
        "teams" : [
                {
                        "employees" : [
                                {
                                        "name" : "john"
                                },
                                {
                                        "name" : "david"
                                }
                        ]
                },
                {
                        "employees" : [
                                {
                                        "name" : "oliver"
                                },
                                {
                                        "name" : "sam"
                                }
                        ]
                }
        ]
}
{
        "teams" : [
                {
                        "employees" : [
                                {
                                        "name" : "singh"
                                },
                                {
                                        "name" : "jane"
                                }
                        ]
                },
                {
                        "employees" : [
                                {
                                        "name" : "marleen"
                                },
                                {
                                        "name" : "florence"
                                }
                        ]
                }
        ]
}

What is wrong with my syntax? I am open to other suggestions to achieve my objective

解决方案

Use

db.departments.aggregate([
    { $unwind: "$teams" },
    { $unwind: "$teams.employees" },
    { $project: { _id: 0, name: "$teams.employees.name" } },
    { $match: { "name": { $regex: /.*o.*/ }} }
]).toArray()

Result

[
    {
        "name" : "john"
    },
    {
        "name" : "oliver"
    },
    {
        "name" : "florence"
    }
]

这篇关于MongoDB返回没有任何父文档的嵌入式文档的嵌入式文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:04