एक स्ट्रिंग से अलग पहला शब्द प्राप्त करने के लिए, आप विशिष्ट () का उपयोग कर सकते हैं। आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं -
> db.distinctFirstWordDemo.insertOne(
{
"_id": 100,
"StudentName":"John",
"StudentFeature": "John is a good player",
"Subject":"MongoDB"
}
);
{ "acknowledged" : true, "insertedId" : 100 }
> db.distinctFirstWordDemo.insertOne(
{
"_id": 101,
"StudentName":"Carol",
"StudentFeature": "Carol is not a good player",
"Subject":"MongoDB"
}
);
{ "acknowledged" : true, "insertedId" : 101 } संग्रह से सभी दस्तावेज़ों को खोजने () विधि की सहायता से प्रदर्शित करें -
> db.distinctFirstWordDemo.find().pretty();
यह निम्नलिखित आउटपुट देगा -
{
"_id" : 100,
"StudentName" : "John",
"StudentFeature" : "John is a good player",
"Subject" : "MongoDB"
}
{
"_id" : 101,
"StudentName" : "Carol",
"StudentFeature" : "Carol is not a good player",
"Subject" : "MongoDB"
} एक स्ट्रिंग से अलग पहला शब्द प्राप्त करने के लिए क्वेरी निम्नलिखित है -
> student = db.distinctFirstWordDemo.distinct("StudentFeature", {"Subject" : "MongoDB"}).map(function(st){
return st.split(" ")[0];
});
[ "John", "Carol" ]
> printjson(student); यह निम्नलिखित आउटपुट देगा -
[ "John", "Carol" ]