$slice ऑपरेटर का उपयोग करें। आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं -
> db.limitAndSliceProjectionDemo.insertOne(
{
"_id" : 101,
"UserName" : "Carol",
"UserAge" : 26,
"UserMesssage" : [
"Hi",
"Hello",
"Bye",
"Awesome",
"Good",
"Bad",
"Nice",
"Good Night",
"Good Morning"
]
}
);
{ "acknowledged" : true, "insertedId" : 101 } खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है -
> db.limitAndSliceProjectionDemo.find().pretty();
यह निम्नलिखित आउटपुट देगा -
{
"_id" : 101,
"UserName" : "Carol",
"UserAge" : 26,
"UserMesssage" : [
"Hi",
"Hello",
"Bye",
"Awesome",
"Good",
"Bad",
"Nice",
"Good Night",
"Good Morning"
]
} फ़ील्ड को सीमित करने और एक साथ स्लाइस प्रोजेक्शन करने के लिए क्वेरी निम्नलिखित है। यहाँ, हम 2 से 4 तक टुकड़े कर रहे हैं -
> db.limitAndSliceProjectionDemo.find({ "UserName" : "Carol" }, {"_id": 0, "UserName":0,"UserAge":0, "UserMesssage": { "$slice": [2,4] } }).pretty(); यह निम्नलिखित आउटपुट देगा -
{ "UserMesssage" : [ "Bye", "Awesome", "Good", "Bad" ] }