हां, $in का उपयोग करना तेज़ है। आइए एक उदाहरण देखें और दस्तावेजों के साथ एक संग्रह बनाएं -
> db.demo653.insertOne({subject:"MySQL"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea04b274deddd72997713c0")
}
> db.demo653.insertOne({subject:"MongoDB"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea04b304deddd72997713c1")
}
> db.demo653.insertOne({subject:"Java"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea04b354deddd72997713c2")
}
> db.demo653.insertOne({subject:"C"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea04b384deddd72997713c3")
}
> db.demo653.insertOne({subject:"C++"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea04b3b4deddd72997713c4")
} संग्रह से सभी दस्तावेज़ों को खोजने () विधि की सहायता से प्रदर्शित करें -
> db.demo653.find();
यह निम्नलिखित आउटपुट देगा -
{ "_id" : ObjectId("5ea04b274deddd72997713c0"), "subject" : "MySQL" }
{ "_id" : ObjectId("5ea04b304deddd72997713c1"), "subject" : "MongoDB" }
{ "_id" : ObjectId("5ea04b354deddd72997713c2"), "subject" : "Java" }
{ "_id" : ObjectId("5ea04b384deddd72997713c3"), "subject" : "C" }
{ "_id" : ObjectId("5ea04b3b4deddd72997713c4"), "subject" : "C++" } $in का उपयोग करने और एकाधिक एकल खोजों की तुलना में तेज़ी से खोज करने के लिए क्वेरी निम्नलिखित है -
> db.demo653.find({subject:{$in:["MySQL","C++","C"]}}); यह निम्नलिखित आउटपुट देगा -
{ "_id" : ObjectId("5ea04b274deddd72997713c0"), "subject" : "MySQL" }
{ "_id" : ObjectId("5ea04b384deddd72997713c3"), "subject" : "C" }
{ "_id" : ObjectId("5ea04b3b4deddd72997713c4"), "subject" : "C++" }