Reputation: 125
I want to merge two different field in a mongoose in sql i can do something like this
select (first-name last-name) as fullname from person_tbl
this would produce something like this
First name Last name Fullname
Smith Bryan Smith Bryan
Joseph Grant Joseph Grant
Diana Blake Diana Blake
How can I do that in the mongooose I am very confused as to how to do that
Upvotes: 2
Views: 1065
Reputation: 3936
In mongoose, use aggregation to achieve concatenation of values of two keys. Let's say, we have a Person
model in the mongoose and firstName
, lastName
are two fields in the document, to get the fullName
:
Person.aggregate([
{$project: {fullName: {$concat: ["$firstName", " ", "$lastName"]}}}
]);
Output:
{ "_id" : ObjectId("5b83d435c671fcae13004e0f"), "fullName" : "Shivam Pandey" }
{ "_id" : ObjectId("5b83d459c671fcae13004e10"), "fullName" : "J. Whit" }
MongoDB Ref: Link
Upvotes: 2