我想在猫鼬中合并两个不同的字段 在SQL中,我可以做这样的事情
select (first-name last-name) as fullname from person_tbl
这会产生这样的东西
First name Last name Fullname
Smith Bryan Smith Bryan
Joseph Grant Joseph Grant
Diana Blake Diana Blake
我如何在猫鼬中做到这一点 我对如何做到这一点感到很困惑
答案 0 :(得分:1)
在猫鼬中,使用聚合来实现两个键的值的串联。假设我们在猫鼬中有一个Person
模型,并且firstName
,lastName
是文档中的两个字段,以获得fullName
:
Person.aggregate([
{$project: {fullName: {$concat: ["$firstName", " ", "$lastName"]}}}
]);
输出:
{ "_id" : ObjectId("5b83d435c671fcae13004e0f"), "fullName" : "Shivam Pandey" }
{ "_id" : ObjectId("5b83d459c671fcae13004e10"), "fullName" : "J. Whit" }
MongoDB参考:Link