Mongodb 如何在mongo查询结果中从内部对象获取值

Mongodb 如何在mongo查询结果中从内部对象获取值,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我是mongodb的新手 我的收藏中有此对象: { "_id" : ObjectId("5b549be38d9f1c00160117d3"), "name" : "Name of object", "a" : { "b" : { "c" : 100 } } } 我感兴趣的是100的值,我想从对象中获取它。 当我像这样查询集合时: db.getCollection('myCollection').find({

我是mongodb的新手

我的收藏中有此对象:

{
    "_id" : ObjectId("5b549be38d9f1c00160117d3"),
    "name" : "Name of object",
    "a" : {
        "b" : {
            "c" : 100
        }
    }
}
我感兴趣的是100的值,我想从对象中获取它。 当我像这样查询集合时:

db.getCollection('myCollection').find({}, {'name':1, 'a.b.c':1})
{"Name": "Name of object", "c":100}
我只得到与内部对象相同的对象。 有没有办法查询它,这样我就可以得到如下结果:

db.getCollection('myCollection').find({}, {'name':1, 'a.b.c':1})
{"Name": "Name of object", "c":100}
通过使用查询,您可以得到结果。在Mongo聚合查询阶段,您可以根据需要添加条件

请尝试此查询,您可能会得到以下结果:

db.myCollection.aggregate({
  $project: {
    "name": "$name",
    "c": "$a.b.c",
    _id: 0
  }
})

正如@Mayuri所建议的,您可以通过使用
.aggregate()
来实现这一点,但如果您想查看
.find()
中的聚合内(Vs)投影之间的差异,请查看:

因此,
$project
的第一件事是它功能更强大,可以接受更多有助于转换字段的功能。但是
.find()
中的投影非常直接,只能接受很少的东西:例如;投影中字段的值可以是以下值之一:

1 or true to include the field in the return documents.
0 or false to exclude the field.
Projection Operators : $, $elemMatch, $slice, $meta
实际发行:

在执行
'a.b.c.时的查询中:投影中的1
意味着在输出中返回
c
字段,因为字段
c
嵌套在
b
a
中,输出结构不会改变&您将在相同的结构下获得
c
值,但如果使用聚合
{$project:{“c”:“$a.b.c”}
这意味着您正在将
“$a.b.c”
的值赋给名为
c
的字段:如何?:因此,当对聚合中的字段使用
$
时,它将访问字段的值,这有助于赋值