Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js MONGO只获取文档的名称,而不获取整个文档_Node.js_Mongodb_Collections - Fatal编程技术网

Node.js MONGO只获取文档的名称,而不获取整个文档

Node.js MONGO只获取文档的名称,而不获取整个文档,node.js,mongodb,collections,Node.js,Mongodb,Collections,查询mongo以仅查找名称:以一种快速的方式, 我不想得到每个文档,然后从每个文档中得到名称 我是mongo的新手, db.company.find()-->这将给出整个文档,它非常大 注;我对公司名称使用了索引&也是唯一的条件。soo-->我认为这应该有助于我快速准确地找到公司名称 很容易,但我不知道怎么做 这是收藏 collection company : --> list of companies in the collection { {

查询mongo以仅查找名称:以一种快速的方式, 我不想得到每个文档,然后从每个文档中得到名称

我是mongo的新手, db.company.find()-->这将给出整个文档,它非常大

注;我对公司名称使用了索引&也是唯一的条件。soo-->我认为这应该有助于我快速准确地找到公司名称 很容易,但我不知道怎么做

这是收藏

collection company :      --> list of companies in the collection    

 {

    {   
       "_id": "5b8ed214b460e7c17c5a33f9",
        "company_location": "USA",
        "company_name": "tesla",         -----> COMPANY NAME: TESLA
        "cars": 
            [
                 {---car 1---} ,
                 {---car 2---} ,
                 {---car n---} 
            ],
    },

    {   
       "_id": "5b8ed214b460e7c17c5a33f9",
        "company_location": "USA",
        "company_name": "gmc",         -----> COMPANY NAME :GMC
        "cars": 
            [
                 {---car 1---} ,
                 {---car 2---} ,
                 {---car n---} 
            ],
    },

    {   
       "_id": "5b8ed214b460e7c17c5a33f9",
        "company_location": "USA",
        "company_name": "bmw",         -----> COMPANY NAME:BMW
        "cars": 
            [
                 {---car 1---} ,
                 {---car 2---} ,
                 {---car n---} 
            ],
    },
    {   
       "_id": "5b8ed214b460e7c17c5a33f9",
        "company_location": "USA",
        "company_name": "audi",         -----> COMPANY NAME: AUDI
        "cars": 
            [
                 {---car 1---} ,
                 {---car 2---} ,
                 {---car n---} 
            ],
    },
}
因此我只想要公司名称列表,而不是使用
db.companys.find()
-->然后通过遍历它们找到每个的名字


我如何做到这一点:fast是必需的,数据是巨大的

您可以使用
.find()
方法的第二个参数来指定:

返回:

{ "company_name" : "gmc" }
{ "company_name" : "tesla" }
...
{ "_id" : null, "company_names" : [ "gmc", "tesla", ... ] }
或者,您可以使用聚合框架获取具有名称数组的单个文档:

db.companies.aggregate([{ $group: { _id: null, company_names: { $push: "$company_name" } } }])
返回:

{ "company_name" : "gmc" }
{ "company_name" : "tesla" }
...
{ "_id" : null, "company_names" : [ "gmc", "tesla", ... ] }

如果你在
company\u name
上有索引,那么第一种方法应该是最快的方法。在这种情况下,您的查询不需要扫描集合,只能使用索引来获取查询数据()。

您可以使用
.find()
方法的第二个参数来指定:

返回:

{ "company_name" : "gmc" }
{ "company_name" : "tesla" }
...
{ "_id" : null, "company_names" : [ "gmc", "tesla", ... ] }
或者,您可以使用聚合框架获取具有名称数组的单个文档:

db.companies.aggregate([{ $group: { _id: null, company_names: { $push: "$company_name" } } }])
返回:

{ "company_name" : "gmc" }
{ "company_name" : "tesla" }
...
{ "_id" : null, "company_names" : [ "gmc", "tesla", ... ] }

如果你在
company\u name
上有索引,那么第一种方法应该是最快的方法。在这种情况下,您的查询不需要扫描集合,只能使用索引获取查询数据()。

哪一个是fast@user6202188MongoDB聚合管道在初始执行时自然会稍微慢一点。但是,find查询可能会比较慢,因为它会批量获取数据。即便如此,这两个命令都不应该特别慢。您最关心的是处理的数据量,因为大量文档的收集可能会超出聚合管道的限制。您最好的选择可能是将
.find()
与投影一起使用。如果您发现自己遇到了性能问题,您可能需要更好的索引。查询投影和适当的索引将允许查询仅使用索引检索结果。这称为“覆盖查询”。看,哪一个是fast@user6202188MongoDB聚合管道在初始执行时自然会稍微慢一点。但是,find查询可能会比较慢,因为它会批量获取数据。即便如此,这两个命令都不应该特别慢。您最关心的是处理的数据量,因为大量文档的收集可能会超出聚合管道的限制。您最好的选择可能是将
.find()
与投影一起使用。如果您发现自己遇到了性能问题,您可能需要更好的索引。查询投影和适当的索引将允许查询仅使用索引检索结果。这称为“覆盖查询”。看见