การรวมสตริง (Concatenate) จากสองฟิลด์เป็นฟิลด์ใหม่ใน MongoDB
ใช้ $concat operator ภายใน $project ของ Aggregation Pipeline เพื่อรวมค่าจากหลายฟิลด์เข้าด้วยกันพร้อมกับตัวคั่น (delimiter)
ไวยากรณ์
db.yourCollectionName.aggregate([
{ $project: {
"yourNewFieldName": {
$concat: [ "$yourFieldName1", " yourDelimiterValue ", "$yourFieldName2" ]
}
}}
]);
ตัวอย่างการสร้างคอลเลกชันและเพิ่มข้อมูล
> db.concatenateStringsDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Doe"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9bb7362d66697741252444") }
> db.concatenateStringsDemo.insertOne({"StudentFirstName":"John","StudentLastName":"Smith"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9bb7402d66697741252445") }
> db.concatenateStringsDemo.insertOne({"StudentFirstName":"Carol","StudentLastName":"Taylor"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9bb74c2d66697741252446") }
> db.concatenateStringsDemo.insertOne({"StudentFirstName":"David","StudentLastName":"Miller"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9bb7752d66697741252447") }
> db.concatenateStringsDemo.insertOne({"StudentFirstName":"James","StudentLastName":"Williams"});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9bb7862d66697741252448") }
แสดงข้อมูลทั้งหมด
> db.concatenateStringsDemo.find().pretty();
ผลลัพธ์:
{ "_id" : ObjectId("5c9bb7362d66697741252444"), "StudentFirstName" : "John", "StudentLastName" : "Doe" }
{ "_id" : ObjectId("5c9bb7402d66697741252445"), "StudentFirstName" : "John", "StudentLastName" : "Smith" }
{ "_id" : ObjectId("5c9bb74c2d66697741252446"), "StudentFirstName" : "Carol", "StudentLastName" : "Taylor" }
{ "_id" : ObjectId("5c9bb7752d66697741252447"), "StudentFirstName" : "David", "StudentLastName" : "Miller" }
{ "_id" : ObjectId("5c9bb7862d66697741252448"), "StudentFirstName" : "James", "StudentLastName" : "Williams" }
คำสั่งรวมสตริง FirstName และ LastName ด้วย " / " เป็นตัวคั่น
> db.concatenateStringsDemo.aggregate([
{ $project: {
"StudentFullName": {
$concat: [ "$StudentFirstName", " / ", "$StudentLastName" ]
}
}}
]);
ผลลัพธ์:
{ "_id" : ObjectId("5c9bb7362d66697741252444"), "StudentFullName" : "John / Doe" }
{ "_id" : ObjectId("5c9bb7402d66697741252445"), "StudentFullName" : "John / Smith" }
{ "_id" : ObjectId("5c9bb74c2d66697741252446"), "StudentFullName" : "Carol / Taylor" }
{ "_id" : ObjectId("5c9bb7752d66697741252447"), "StudentFullName" : "David / Miller" }
{ "_id" : ObjectId("5c9bb7862d66697741252448"), "StudentFullName" : "James / Williams" }
คำอธิบาย $concat
- รับ array ของ strings/expressions
- รวมกันตามลำดับใน array
- ถ้าฟิลด์ใดเป็น null จะคืนค่า null ทั้งหมด (ต้องจัดการด้วย $ifNull)
ตัวอย่างขั้นสูง: จัดการค่า null และเพิ่มฟิลด์เดิม
> db.concatenateStringsDemo.aggregate([
{ $project: {
"StudentFirstName": 1,
"StudentLastName": 1,
"StudentFullName": {
$concat: [
{ $ifNull: ["$StudentFirstName", ""] },
" / ",
{ $ifNull: ["$StudentLastName", ""] }
]
}
}}
]);
การใช้งานร่วมกับ $addFields (MongoDB 3.4+)
ถ้าต้องการเพิ่มฟิลด์ใหม่แต่เก็บฟิลด์เดิมไว้ทั้งหมด:
> db.concatenateStringsDemo.aggregate([
{ $addFields: {
"StudentFullName": {
$concat: [ "$StudentFirstName", " / ", "$StudentLastName" ]
}
}}
]);
การอัปเดตข้อมูลจริงใน Collection (Persistent)
Aggregation แค่แสดงผล ถ้าต้องการบันทึกลงฐานข้อมูล:
> db.concatenateStringsDemo.find().forEach(function(doc) {
db.concatenateStringsDemo.updateOne(
{ _id: doc._id },
{ $set: { "StudentFullName": doc.StudentFirstName + " / " + doc.StudentLastName } }
);
});
หมายเหตุ: $concat ใช้ได้เฉพาะใน Aggregation Pipeline ไม่สามารถใช้ใน find() หรือ update() โดยตรงได้