अल्फ़ान्यूमेरिक सॉर्ट के लिए numericOrdering: true सेट करने की आवश्यकता होती है। पहले दस्तावेजों के साथ एक संग्रह बनाते हैं:
> db.alphanumericSortDemo.insertOne({"StudentId":"STU1010"});
{ "acknowledged" : true, "insertedId" : ObjectId("5ccf149adceb9a92e6aa194c") }
> db.alphanumericSortDemo.insertOne({"StudentId":"STU1101"});
{ "acknowledged" : true, "insertedId" : ObjectId("5ccf14a2dceb9a92e6aa194d") }
> db.alphanumericSortDemo.insertOne({"StudentId":"STU1901"});
{ "acknowledged" : true, "insertedId" : ObjectId("5ccf14a9dceb9a92e6aa194e") }
> db.alphanumericSortDemo.insertOne({"StudentId":"STU908"});
{ "acknowledged" : true, "insertedId" : ObjectId("5ccf14aedceb9a92e6aa194f") }
> db.alphanumericSortDemo.insertOne({"StudentId":"STU101"});
{ "acknowledged" : true, "insertedId" : ObjectId("5ccf14b2dceb9a92e6aa1950") }
संग्रह से सभी दस्तावेजों को find() विधि की सहायता से प्रदर्शित करने के लिए क्वेरी:
> db.alphanumericSortDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
{ "_id" : ObjectId("5ccf149adceb9a92e6aa194c"), "StudentId" : "STU1010" }
{ "_id" : ObjectId("5ccf14a2dceb9a92e6aa194d"), "StudentId" : "STU1101" }
{ "_id" : ObjectId("5ccf14a9dceb9a92e6aa194e"), "StudentId" : "STU1901" }
{ "_id" : ObjectId("5ccf14aedceb9a92e6aa194f"), "StudentId" : "STU908" }
{ "_id" : ObjectId("5ccf14b2dceb9a92e6aa1950"), "StudentId" : "STU101" }
केस 1 - आरोही क्रम में परिणाम
MongoDB में अल्फ़ान्यूमेरिक सॉर्ट करने के लिए क्वेरी:
> db.alphanumericSortDemo.find({}).sort({"StudentId" : 1}).collation( { locale: "en_US", numericOrdering: true });
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
{ "_id" : ObjectId("5ccf14b2dceb9a92e6aa1950"), "StudentId" : "STU101" }
{ "_id" : ObjectId("5ccf14aedceb9a92e6aa194f"), "StudentId" : "STU908" }
{ "_id" : ObjectId("5ccf149adceb9a92e6aa194c"), "StudentId" : "STU1010" }
{ "_id" : ObjectId("5ccf14a2dceb9a92e6aa194d"), "StudentId" : "STU1101" }
{ "_id" : ObjectId("5ccf14a9dceb9a92e6aa194e"), "StudentId" : "STU1901" }
केस 2 - अवरोही क्रम में परिणाम
MongoDB में अल्फ़ान्यूमेरिक सॉर्ट करने के लिए क्वेरी:
> db.alphanumericSortDemo.find({}).sort({"StudentId" : -1}).collation( { locale: "en_US", numericOrdering: true });
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
{ "_id" : ObjectId("5ccf14a9dceb9a92e6aa194e"), "StudentId" : "STU1901" }
{ "_id" : ObjectId("5ccf14a2dceb9a92e6aa194d"), "StudentId" : "STU1101" }
{ "_id" : ObjectId("5ccf149adceb9a92e6aa194c"), "StudentId" : "STU1010" }
{ "_id" : ObjectId("5ccf14aedceb9a92e6aa194f"), "StudentId" : "STU908" }
{ "_id" : ObjectId("5ccf14b2dceb9a92e6aa1950"), "StudentId" : "STU101" }