在MongoDB中搜索具有相似数组的文档,并按相似度值排序
让我们创建一个包含文档的集合-
> db.demo123.insertOne({"ListOfSubject":['MySQL', 'MongoDB', 'Java']}); { "acknowledged" : true, "insertedId" : ObjectId("5e2f24ac140daf4c2a3544b8") } > db.demo123.insertOne({"ListOfSubject":['Python', 'MongoDB', 'C']}); { "acknowledged" : true, "insertedId" : ObjectId("5e2f24cd140daf4c2a3544b9") } > db.demo123.insertOne({"ListOfSubject":['MySQL', 'MongoDB', 'C++']}); { "acknowledged" : true, "insertedId" : ObjectId("5e2f24ce140daf4c2a3544ba") }
在find()方法的帮助下显示集合中的所有文档-
> db.demo123.find();
这将产生以下输出-
{ "_id" : ObjectId("5e2f24ac140daf4c2a3544b8"), "ListOfSubject" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e2f24cd140daf4c2a3544b9"), "ListOfSubject" : [ "Python", "MongoDB", "C" ] } { "_id" : ObjectId("5e2f24ce140daf4c2a3544ba"), "ListOfSubject" : [ "MySQL", "MongoDB", "C++" ] }
以下是搜索具有相似数组的文档并对其进行排序的查询-
> var subjects = ['MySQL', 'MongoDB', 'Java']; > db.demo123.aggregate([ ... {$unwind: "$ListOfSubject"}, ... {$match: {ListOfSubject:{ $in:subjects}}}, ... {$group: {_id: "$_id", number: {$sum: 1}}}, ... {$project: {_id: 1, number: 1, percentage: {$divide: ["$number",subjects.length]}}}, ... {$sort: {percentage: -1}} ... ]);
这将产生以下输出-
{ "_id" : ObjectId("5e2f24ac140daf4c2a3544b8"), "number" : 3, "percentage" : 1 } { "_id" : ObjectId("5e2f24ce140daf4c2a3544ba"), "number" : 2, "percentage" : 0.6666666666666666 } { "_id" : ObjectId("5e2f24cd140daf4c2a3544b9"), "number" : 1, "percentage" : 0.3333333333333333 }