कम से कम एक मैच पाने के लिए $in ऑपरेटर का उपयोग करें। आइए पहले दस्तावेजों के साथ एक संग्रह बनाएं -
> db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["MySQL","MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2db5db64f4b851c3a13ce") } > db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["Java","C","MongoDB"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2db71b64f4b851c3a13cf") } > db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["Python","C++","SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2db87b64f4b851c3a13d0") } >db.atleastOneMatchDemo.insertOne({"StudentFavouriteSubject":["Ruby","Javascript","C#","MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd2dba9b64f4b851c3a13d1") }
खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है -
> db.atleastOneMatchDemo.find().pretty();
यह निम्नलिखित आउटपुट देगा -
{ "_id" : ObjectId("5cd2db5db64f4b851c3a13ce"), "StudentFavouriteSubject" : [ "MySQL", "MongoDB" ] } { "_id" : ObjectId("5cd2db71b64f4b851c3a13cf"), "StudentFavouriteSubject" : [ "Java", "C", "MongoDB" ] } { "_id" : ObjectId("5cd2db87b64f4b851c3a13d0"), "StudentFavouriteSubject" : [ "Python", "C++", "SQL Server" ] } { "_id" : ObjectId("5cd2dba9b64f4b851c3a13d1"), "StudentFavouriteSubject" : [ "Ruby", "Javascript", "C#", "MySQL" ] }
कम से कम एक मैच पाने के लिए क्वेरी निम्नलिखित है -
>db.atleastOneMatchDemo.find({"StudentFavouriteSubject":{"$in":["MongoDB","MySQL"]}}).pretty();
यह निम्नलिखित आउटपुट देगा -
{ "_id" : ObjectId("5cd2db5db64f4b851c3a13ce"), "StudentFavouriteSubject" : [ "MySQL", "MongoDB" ] } { "_id" : ObjectId("5cd2db71b64f4b851c3a13cf"), "StudentFavouriteSubject" : [ "Java", "C", "MongoDB" ] } { "_id" : ObjectId("5cd2dba9b64f4b851c3a13d1"), "StudentFavouriteSubject" : [ "Ruby", "Javascript", "C#", "MySQL" ] }