इसके लिए मैपरेडस () का इस्तेमाल करें। आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं -
> db.splitString.insertOne({"StudentName":"John Smith"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e0849d925ddae1f53b62206")
} खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है -
> db.splitString.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{
"_id" : ObjectId("5e0849d925ddae1f53b62206"),
"StudentName" : "John Smith"
} यहाँ एक स्ट्रिंग को विभाजित करने की क्वेरी है -
> db.splitString.mapReduce(
... function() {
... var StudentLastName = this.StudentName.split(/\s/).reverse()[0].toUpperCase();
...
... emit({ "StudentLastName": StudentLastName, "FirstObjectId": this._id },this);
... },
... function(){},
... { "out": { "inline": 1 } }
... ); यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{
"results" : [
{
"_id" : {
"StudentLastName" : "SMITH",
"FirstObjectId" : ObjectId("5e0849d925ddae1f53b62206")
},
"value" : {
"_id" : ObjectId("5e0849d925ddae1f53b62206"),
"StudentName" : "John Smith"
}
}
],
"timeMillis" : 32,
"counts" : {
"input" : 1,
"emit" : 1,
"reduce" : 0,
"output" : 1
},
"ok" : 1
}