如何知道mongodb查询有多less匹配

在我的项目中,我有一个search栏,用户可以search其他用户。

我使用MongoDB作为我的数据库引擎,在我的服务器nodeJSmongoose,到目前为止,一切正常,除了一件事情,我想按相关性sorting的结果。

想象一下,我有这个集合

{ "nombre" : "Ramiro", "apellido" : "Garcilazo", "empresa" : "PEMEX", "tamano" : "Grande(250+)", "sector" : "Servicios", "giro" : "Sustancias Químicas", "actividad" : "Venta de petroquimicos", "estado" : "Jalisco" } { "nombre" : "Luis", "apellido" : "Alberto", "empresa" : "GanaMex", "tamano" : "Mediana (51-250)", "sector" : "Construccion", "giro" : "Agricultura, Ganaderia y Pesca", "actividad" : "Graneros", "estado" : "Aguascalientes" } { "nombre" : "Ramon", "apellido" : "Corona", "empresa" : "CoronMex", "tamano" : "Micro (1-10)", "sector" : "Construccion", "giro" : "Textiles, Prendas y Productos de Cuero", "actividad" : "Venta de tiendas de campaña", "estado" : "Ciudad de mexico" } { "nombre" : "Joe", "apellido" : "Doe", "empresa" : "Apple inc", "tamano" : "Micro (1-10)", "sector" : "Construccion", "giro" : "Alimentos y Bebidas", "actividad" : "Ejemplo", "estado" : "Veracruz" } { "nombre" : "John", "apellido" : "Smith", "empresa" : "Google inc", "tamano" : "Micro (1-10)", "sector" : "Bienes", "giro" : "Agricultura, Ganaderia y Pesca", "actividad" : "XYZ", "estado" : "Aguascalientes" } { "nombre" : "foo", "apellido" : "bar", "empresa" : "foobar inc.", "tamano" : "Mediana (51-250)", "sector" : "Servicios", "giro" : "Alimentos y Bebidas", "actividad" : "foo-bar", "estado" : "Aguascalientes" } { "nombre" : "Jonathan", "apellido" : "Ceja", "empresa" : "It4Pymes", "tamano" : "Grande(250+)", "sector" : "Bienes", "giro" : "Mineria, Petroleó y Gas", "actividad" : "asdf", "estado" : "Baja California" } 

而我目前的查询看起来像这样

 export function buscarProveedor(req, res) { var regTerms = []; //creates an RegExp array of all the terms the user searched var terms = req.body.term.split(" "); for (var i = 0; i < terms.length; i++) { regTerms.push(new RegExp(terms[i], 'i')); } //creates a single RegExp for all the term var regTerm = new RegExp(req.body.term, 'i'); User.find({ $and: [{ //any of this that matches $or: [{ nombre: regTerm }, { empresa: regTerm }, { sector: { $in: regTerms } }, { giro: { $in: regTerms } }, { estado: { $in: regTerms } }, { actividad: { $in: regTerms } }, { tags: { $in: regTerms } }] }, { //ignore this, just checks if is not another type of user empresa: { $exists: true } }, { //checks that doesn't gets the user that made the query _id: { $ne: req.body.id } }] }, '_id nombre empresa') .then(user => { //returns it to my front-end res.json(user).end(); }); } 

所以,例如在我的search栏中input“Construccion”,回应就是

 { "_id": 0 "nombre" : "Luis", "empresa" : "GanaMex" } { "_id": 1 "nombre" : "Ramon", "empresa" : "CoronMex", } { "_id": 2 "nombre" : "Joe", "empresa" : "Apple inc", } 

但是,如果我把“Construccion Textiles”,我会得到相同的确切结果,即使id 1匹配2项。

我想知道它有多less匹配,所以我可以通过相关性sorting,在顶部find更多的匹配。

我可以通过查询来实现这个function吗?或者我不得不在操作数据之后操作数据,我真的不知道,而且我以前从未见过这个问题。

谢谢。


编辑 :好吧我用$匹配和$组来做到这一点,但仍然没有做我想要的,我用这个。

 export function buscarProveedorAvansado(req, res) { var regTerms = []; var terms = req.body.term.split(" "); for (var i = 0; i < terms.length; i++) { regTerms.push(new RegExp(terms[i], 'i')); } var regTerm = new RegExp(req.body.term, 'i'); User.aggregate([{ $match: { $and: [{ $or: [{ nombre: regTerm }, { empresa: regTerm }, { sector: {$in: regTerms} }, { giro: {$in: regTerms} }, { estado: {$in: regTerms} }, { actividad: {$in: regTerms} }, { tags: {$in: regTerms} }] }, { empresa: {$exists: true} }, { _id: { $ne: req.body.id } }] } }, { $group: { "_id" : "$_id", "nombre" : {$max: "$nombre"}, "empresa": {$max: "$empresa"}, count: { $sum: 1 } } }]).then(user => { console.log(user); res.json(user).end(); }); } 

我使用“Construccion”这个词来得到这个结果

 [{ "_id": 0 "nombre" : "Luis", "empresa" : "GanaMex", "count": 1 } { "_id": 1 "nombre" : "Ramon", "empresa" : "CoronMex", "count": 1 } { "_id": 2 "nombre" : "Joe", "empresa" : "Apple inc", "count": 1 }] 

而且,如果我使用“Construccion Textiles”一词,我会得到完全相同的结果。

我已经尝试了几个东西,$ sum上使用$ cond:

  $sum:[{ $cond: { if: { nombre: regTerm }, then: 1, else: 0 } }, { $cond: { if: { empresa: regTerm }, then: 1, else: 0 } }, { $cond: { if: { sector: { $in: regTerms } }, then: 1, else: 0 } }, { $cond: { if: { giro: { $in: regTerms } }, then: 1, else: 0 } }, { $cond: { if: { giro: { $in: regTerms } }, then: 1, else: 0 } }, { $cond: { if: { estado: { $in: regTerms } }, then: 1, else: 0 } }, { $cond: { if: { nombre: regTerm }, then: 1, else: 0 } }, { $cond: { if: { nombre: regTerm }, then: 1, else: 0 } }] 

得到一个mongo错误。

我认为是$和$和$或者是技术上的问题,因为最后是单一的真实,我不知道是否有解决方法。

我在这里错过了明显吗?

您可以使用匹配函数对查询拉回的所有文档执行计数。

https://docs.mongodb.com/manual/reference/operator/aggregation/match/#perform-a-count