I have a very basic question:
SELECT name, surname CONCAT(name, surname) AS name_surname from users;
How can I convert this SQL
to MongoDB
query?
During my search, I have decided that it is possible with aggregate framework due to concat
, but what I received is only projection
of concat(name, surname)
not name
, surname
and concat(name, surname)
.
Final thing I got is this query:
db.inroamers.find().forEach(
function(o) {
print(o.LAC + '-' + o.CELL + ' ' + o.CHARGE + ' ' + o.WEEK);
})
but it does not give me proper json
array.
Any suggestions?
I have a very basic question:
SELECT name, surname CONCAT(name, surname) AS name_surname from users;
How can I convert this SQL
to MongoDB
query?
During my search, I have decided that it is possible with aggregate framework due to concat
, but what I received is only projection
of concat(name, surname)
not name
, surname
and concat(name, surname)
.
Final thing I got is this query:
db.inroamers.find().forEach(
function(o) {
print(o.LAC + '-' + o.CELL + ' ' + o.CHARGE + ' ' + o.WEEK);
})
but it does not give me proper json
array.
Any suggestions?
Share Improve this question edited Dec 17, 2015 at 23:03 BatScream 19.7k4 gold badges53 silver badges68 bronze badges asked Nov 20, 2014 at 8:53 SunubaSunuba 702 silver badges12 bronze badges1 Answer
Reset to default 8Use the aggregation operations as below:
db.collection.aggregate([
{$project:{"name_surname":{$concat:["$name","-","$surname"]},"name":1,"surname":1}}
])