$inc की सीमा निर्धारित करने के लिए, नीचे दिए गए सिंटैक्स का उपयोग करें -
db.yourCollectionName.update({yourFieldName : {$lt : yourValue}}, {$inc : {yourFieldName : yourIncrementValue}},false,true); आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं -
> db.limitIncrementDemo.insertOne({"StudentId":101,"StudentScore":95});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2ce9eb64f4b851c3a13c3")
}
> db.limitIncrementDemo.insertOne({"StudentId":102,"StudentScore":55});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2cea0b64f4b851c3a13c4")
}
> db.limitIncrementDemo.insertOne({"StudentId":103,"StudentScore":67});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2cea1b64f4b851c3a13c5")
}
> db.limitIncrementDemo.insertOne({"StudentId":104,"StudentScore":56});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2cea3b64f4b851c3a13c6")
}
> db.limitIncrementDemo.insertOne({"StudentId":105,"StudentScore":79});
{
"acknowledged" : true,
"insertedId" : ObjectId("5cd2cea4b64f4b851c3a13c7")
} खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है -
> db.limitIncrementDemo.find().pretty();
यह निम्नलिखित आउटपुट देगा -
{
"_id" : ObjectId("5cd2ce9eb64f4b851c3a13c3"),
"StudentId" : 101,
"StudentScore" : 95
}
{
"_id" : ObjectId("5cd2cea0b64f4b851c3a13c4"),
"StudentId" : 102,
"StudentScore" : 55
}
{
"_id" : ObjectId("5cd2cea1b64f4b851c3a13c5"),
"StudentId" : 103,
"StudentScore" : 67
}
{
"_id" : ObjectId("5cd2cea3b64f4b851c3a13c6"),
"StudentId" : 104,
"StudentScore" : 56
}
{
"_id" : ObjectId("5cd2cea4b64f4b851c3a13c7"),
"StudentId" : 105,
"StudentScore" : 79
} $inc की सीमा निर्धारित करने के लिए क्वेरी निम्नलिखित है -
> db.limitIncrementDemo.update({StudentScore : {$lt : 75}}, {$inc : {StudentScore : 10}},false,true);
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 }) आइए उपरोक्त संग्रह से सभी दस्तावेजों की जांच करें -
> db.limitIncrementDemo.find().pretty();
यह निम्नलिखित आउटपुट देगा -
{
"_id" : ObjectId("5cd2ce9eb64f4b851c3a13c3"),
"StudentId" : 101,
"StudentScore" : 95
}
{
"_id" : ObjectId("5cd2cea0b64f4b851c3a13c4"),
"StudentId" : 102,
"StudentScore" : 65
}
{
"_id" : ObjectId("5cd2cea1b64f4b851c3a13c5"),
"StudentId" : 103,
"StudentScore" : 77
}
{
"_id" : ObjectId("5cd2cea3b64f4b851c3a13c6"),
"StudentId" : 104,
"StudentScore" : 66
}
{
"_id" : ObjectId("5cd2cea4b64f4b851c3a13c7"),
"StudentId" : 105,
"StudentScore" : 79
}