Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB

การดึงสตริงย่อย (Substring) ใน MongoDB Aggregate

ใช้ $substr ใน $project

ตัวดำเนินการ $substr (หรือ $substrBytes/$substrCP) ใช้ดึงส่วนของสตริง ตำแหน่งเริ่มต้นและความยาว

> db.demo176.insertOne({"ProductName": "PRODUCT-102"});

คำสั่งดึง 7 ตัวอักษรแรก:

db.demo176.aggregate([ { $project: { ProductName: { $substr: ["$ProductName", 0, 7] } } } ])

ผลลัพธ์: ทุกเอกสารจะมี ProductName = "PRODUCT"