ใช้ distinct() ร่วมกับ map()
เพื่อดึงคำแรกที่ไม่ซ้ำกันจากฟิลด์สตริง ให้ใช้ distinct() แล้วประมวลผลด้วย map() และ split()
> db.distinctFirstWordDemo.insertOne({ "_id": 100, "StudentName": "John", "StudentFeature": "John is a good player", "Subject": "MongoDB" });
คำสั่งดึงคำแรกที่ไม่ซ้ำ:
> student = db.distinctFirstWordDemo.distinct("StudentFeature", {"Subject": "MongoDB"}).map(function(st){ return st.split(" ")[0]; });
ผลลัพธ์: ["John", "Carol"]