স্ট্রিংয়ের প্রথম শব্দের ডিস্টিনκτ মান বের করতে split() ব্যবহার করুন। প্রথমে ডকুমেন্টসহ একটি কলেকশন তৈরি করি:
> db.demo129.insertOne({"Words":"This is the MySQL","CountryName":"US"});
{ "acknowledged" : true, "insertedId" : ObjectId("5e305d6368e7f832db1a7f6b") }
> db.demo129.insertOne({"Words":"MongoDB is NOSQL database","CountryName":"US"});
{ "acknowledged" : true, "insertedId" : ObjectId("5e305d7b68e7f832db1a7f6c") }
find() মেথড দিয়ে সব ডকুমেন্ট দেখান:
> db.demo129.find();
আউটপুট:
{ "_id" : ObjectId("5e305d6368e7f832db1a7f6b"), "Words" : "This is the MySQL", "CountryName" : "US" }
{ "_id" : ObjectId("5e305d7b68e7f832db1a7f6c"), "Words" : "MongoDB is NOSQL database", "CountryName" : "US" }
স্ট্রিংয়ের প্রথম শব্দের ডিস্টিনکت মান বের করার কোয়েরি:
> w = db.demo129.distinct("Words", {"CountryName" : "US"}).map(function(doc){ return doc.split(" ")[0]; });
[ "This", "MongoDB" ]
printjson() দিয়ে দেখান:
> printjson(w);
ফলাফল:
[ "This", "MongoDB" ]