लंबाई मानदंड के साथ मोंगोडीबी से पूछताछ करने के लिए, आप रेगेक्स का उपयोग कर सकते हैं। निम्नलिखित वाक्य रचना है
db.yourCollectionName.find({ ‘yourFieldName’: { $regex: /^.{yourLengthValue1,yourLengthValue2}$/ } }); आइए दस्तावेजों के साथ एक संग्रह बनाएं। निम्नलिखित प्रश्न है
> db.queryLengthDemo.insertOne({"StudentFullName":"John Smith"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a01ae353decbc2fc927c0")
}
> db.queryLengthDemo.insertOne({"StudentFullName":"John Doe"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a01b4353decbc2fc927c1")
}
> db.queryLengthDemo.insertOne({"StudentFullName":"David Miller"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a01c2353decbc2fc927c2")
}
> db.queryLengthDemo.insertOne({"StudentFullName":"Robert Taylor"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a01e2353decbc2fc927c3")
}
> db.queryLengthDemo.insertOne({"StudentFullName":"Chris Williams"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9a01f1353decbc2fc927c4")
} खोज () विधि की सहायता से संग्रह से सभी दस्तावेज़ों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है
> db.queryLengthDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा
{
"_id" : ObjectId("5c9a01ae353decbc2fc927c0"),
"StudentFullName" : "John Smith"
}
{
"_id" : ObjectId("5c9a01b4353decbc2fc927c1"),
"StudentFullName" : "John Doe"
}
{
"_id" : ObjectId("5c9a01c2353decbc2fc927c2"),
"StudentFullName" : "David Miller"
}
{
"_id" : ObjectId("5c9a01e2353decbc2fc927c3"),
"StudentFullName" : "Robert Taylor"
}
{
"_id" : ObjectId("5c9a01f1353decbc2fc927c4"),
"StudentFullName" : "Chris Williams"
} लंबाई मानदंड के साथ MongoDB में क्वेरी निम्नलिखित है
> db.queryLengthDemo.find({ StudentFullName: { $regex: /^.{9,12}$/ } }).pretty(); यह निम्नलिखित आउटपुट उत्पन्न करेगा
{
"_id" : ObjectId("5c9a01ae353decbc2fc927c0"),
"StudentFullName" : "John Smith"
}
{
"_id" : ObjectId("5c9a01c2353decbc2fc927c2"),
"StudentFullName" : "David Miller"
}