এই ধরনের আনার জন্য, শুধুমাত্র $nin এবং $in ব্যবহার করুন। আসুন নথি-
সহ একটি সংগ্রহ তৈরি করি> db.demo140.insertOne({"Id":101,"Subjects":["MongoDB","MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31c149fdf09dd6d08539a9") } > db.demo140.insertOne({"Id":102,"Subjects":["MongoDB","Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31c14cfdf09dd6d08539aa") } > db.demo140.insertOne({"Id":103,"Subjects":["MongoDB","PL/SQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31c157fdf09dd6d08539ab") } > db.demo140.insertOne({"Id":104,"Subjects":["MongoDB","SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31c163fdf09dd6d08539ac") } > db.demo140.insertOne({"Id":105,"Subjects":["C","C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e31c16ffdf09dd6d08539ad") }
Find() পদ্ধতি -
এর সাহায্যে একটি সংগ্রহ থেকে সমস্ত নথি প্রদর্শন করুন> db.demo140.find();
এটি নিম্নলিখিত আউটপুট −
তৈরি করবে{ "_id" : ObjectId("5e31c149fdf09dd6d08539a9"), "Id" : 101, "Subjects" : [ "MongoDB", "MySQL" ] } { "_id" : ObjectId("5e31c14cfdf09dd6d08539aa"), "Id" : 102, "Subjects" : [ "MongoDB", "Java" ] } { "_id" : ObjectId("5e31c157fdf09dd6d08539ab"), "Id" : 103, "Subjects" : [ "MongoDB", "PL/SQL" ] } { "_id" : ObjectId("5e31c163fdf09dd6d08539ac"), "Id" : 104, "Subjects" : [ "MongoDB", "SQL Server" ] } { "_id" : ObjectId("5e31c16ffdf09dd6d08539ad"), "Id" : 105, "Subjects" : [ "C", "C++" ] }
এখানে "MongoDB" বিষয় আছে কিন্তু "Java" নয় -
> db.demo140.find({$and:[{Subjects:{$in:['MongoDB']}}, {Subjects:{$nin:['Java']}}]}).toArray();
এটি নিম্নলিখিত আউটপুট −
তৈরি করবে[ { "_id" : ObjectId("5e31c149fdf09dd6d08539a9"), "Id" : 101, "Subjects" : [ "MongoDB", "MySQL" ] }, { "_id" : ObjectId("5e31c157fdf09dd6d08539ab"), "Id" : 103, "Subjects" : [ "MongoDB", "PL/SQL" ] }, { "_id" : ObjectId("5e31c163fdf09dd6d08539ac"), "Id" : 104, "Subjects" : [ "MongoDB", "SQL Server" ] } ]