Sử dụng distinct() kết hợp map() và split() để lấy từ đầu tiên duy nhất từ trường chuỗi.
Tạo collection demo129:
> db.demo129.insertOne({"Words":"This is the MySQL","CountryName":"US"});
> db.demo129.insertOne({"Words":"MongoDB is NOSQL database","CountryName":"US"});
Lấy distinct Words cho CountryName=US, split lấy từ đầu tiên:
> w = db.demo129.distinct("Words", {"CountryName":"US"}).map(function(doc){
return doc.split(" ")[0];
});
[ "This", "MongoDB" ]
Hiển thị kết quả:
> printjson(w);
[ "This", "MongoDB" ]