Mongodb在查询中创建别名 [英] Mongodb creating alias in a query
本文介绍了Mongodb在查询中创建别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此查询等效于mongodb:
What is the mongodb's equivalent to this query:
从表名中选择"foo"作为栏,将
id
作为"spec"
SELECT "foo" as bar,
id
as "spec" from tablename
推荐答案
可以使用$ project创建具有给定名称和值的新字段,该字段取自另一个字段:
It is possible to create new field with given name and value taken from another field with $project:
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
接下来的$ project阶段添加了新字段isbn,lastName和copysSold:
The following $project stage adds the new fields isbn, lastName, and copiesSold:
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/聚合/项目/#pipe._S_project
这篇关于Mongodb在查询中创建别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文