Mongodb 在查询中创建别名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7093441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:12:36  来源:igfitidea点击:

Mongodb creating alias in a query

mongodb

提问by Fatmuemoo

What is the mongodb's equivalent to this query:

mongodb 与此查询的等价物是什么:

SELECT "foo" as bar, idas "spec" from tablename

SELECT "foo" as bar, idas "spec" from tablename

回答by Andrey Dolgikh

It is possible to create new field with given name and value taken from another field with $project:

可以使用 $project 使用从另一个字段中获取的给定名称和值创建新字段:

{
  "_id" : 1,
  title: "abc123",
  isbn: "0001122223334",
  author: { last: "zzz", first: "aaa" },
  copies: 5
}

The following $project stage adds the new fields isbn, lastName, and copiesSold:

以下 $project 阶段添加了新字段 isbn、lastName 和 copySold:

db.books.aggregate(
   [
      {
         $project: {
            title: 1,
            isbn: {
               prefix: { $substr: [ "$isbn", 0, 3 ] },
               group: { $substr: [ "$isbn", 3, 2 ] },
               publisher: { $substr: [ "$isbn", 5, 4 ] },
               title: { $substr: [ "$isbn", 9, 3 ] },
               checkDigit: { $substr: [ "$isbn", 12, 1] }
            },
            lastName: "$author.last",
            copiesSold: "$copies"
         }
      }
   ]
)

http://docs.mongodb.org/manual/reference/operator/aggregation/project/#pipe._S_project

http://docs.mongodb.org/manual/reference/operator/aggregation/project/#pipe._S_project

回答by user1735921

You can use any operator like toUpper or toLower or concat or any other operator you feel like which you think you can work on and create an alias.

您可以使用任何运算符,如 toUpper 或 toLower 或 concat,或者您认为可以使用并创建别名的任何其他运算符。

Example: In the following example created_time is a field in the collection. (I am not good with syntax so you can correct it, but this is the approach)

示例:在以下示例中 created_time 是集合中的一个字段。(我不擅长语法,所以你可以纠正它,但这是方法)

{$project {
"ALIAS_one" : {"$concat" : "$created_time"},
"ALIAS_two" : {"$concat" : "$created_time"},
"ALIAS_three" : {"$concat" : "$created_time"}
}}

So using an operator in that fashion you can create as many as aliases as your like.

因此,以这种方式使用运算符,您可以创建任意数量的别名。

回答by Edward

you can use this, maybe help

你可以用这个,也许有帮助

database data

数据库数据

{ "_id" : "5ab0f445edf197158835be63", "userid" : "5aaf15c28264ee17fe869ad8", "lastmodified" : ISODate("2018-03-21T07:04:41.735Z") }
{ "_id" : "5ab0f445edf197158835be64", "userid" : "5aaf15c28264ee17fe869ad8", "lastmodified" : ISODate("2018-02-20T12:31:08.896Z") }
{ "_id" : "5ab0f445edf197158835be65", "userid" : "5aaf15c28264ee17fe869ad7", "lastmodified" : ISODate("2018-02-20T02:31:08.896Z") }

mongo command

mongo 命令

db.zhb_test.aggregate(
[{
    $group: {
        _id: {
            $dateToString: {
                format: "%Y-%m",
                date: "$lastmodified"
            }
        },
        count: {
            $sum: 1
        }
    }
},
{
    $project: {
        "month": "$_id",
        count: 1,
        "_id": 0
    }
}])

result

结果

{ "count" : 2, "month" : "2018-02" }
{ "count" : 1, "month" : "2018-03" }