如何对猫鼬查询进行排序

时间:2018-10-05 20:14:11

标签: mongodb mongoose

我正在使用NodeJS,IBM Watson和Mongoose(MongoDB)为大学做一个Web报废项目。我的数据库中有以下文档:

  • 新闻集
  • 每个新闻都有一组实体
  • 每个实体都有dectype,文本和相关性

文档:

"news":[  
    {  
        "uuidXtract":"a0ea8aa2f1-a6f3c-a24af-ac4f2-aa6b1a50fea3444",
        "title":"CITec-Marília marca presença em evento internacional",
        "linkPub":"http://www.inovamarilia.com.br/2018/06/04/citec-marilia-marca-presenca-em-evento-internacional/",
        "datePub":"4 de junho de 2018",
        "description":"Prof. Dr. Elvis Fusco, coordenador do CITec-Marília, durante apresentação dos ambientes de inovação no China National Convention Center",
        "categories":[  
            "Notícias"
        ],
        "entities":[  
            {  
                "text":"Prof. Dr. Elvis Fusco",
                "relevance":0.954695,
                "count":3,
                "desctype":"Person"
            },
            {  
                "text":"Gao Feng",
                "relevance":0.779051,
                "count":2,
                "desctype":"Person"
            },
            {  
                "text":"Centro Nacional de Convenções da China",
                "relevance":0.659293,
                "count":1,
                "desctype":"Organization"
            }
        ]
    }
]

我很难创建一个查询,该查询返回实体为 desctype ='Person'的所有文档的 title linkPub 并且实体 text ='教授。 Elvis Fusco博士”。。这些文档应按相关性排序

假设新闻是猫鼬模型对象,我尝试了以下代码:

news.find({}).
where('entities.desctype').equals('Person').
  where('entities.text').equals('Prof. Dr. Elvis Fusco').
  sort('entities.relevance').
  select('title linkPub entities.text entities.relevance').
  exec(function (err, docs) {
    for(i in docs) {
      console.log(docs[i]);
    }
    mongoose.connection.close();
  });

退回文件示例:

{  
"_id":"5bb798c43b6f721bc076dde5",
"title":"CITec-Marília atrai fundo de investimento de 3,3 milhões para startups",
"linkPub":"http://www.inovamarilia.com.br/2017/04/24/citec-marilia-atrai-fundo-de-investimento-de-33-milhoes-para-startups/",
"entities":[  
    {  
        "text":"Marília",
        "relevance":0.963296
    },
    {  
        "text":"CIEM",
        "relevance":0.593319
    },
    {  
        "text":"Geroldo",
        "relevance":0.551226
    },
    {  
        "text":"Centro de Inovação Tecnológica de Marília",
        "relevance":0.473857
    },
    {  
        "text":"Centro Universitário Eurípides",
        "relevance":0.447737
    },
    {  
        "text":"Centro Incubador de Empresas de Marília",
        "relevance":0.438098
    },
    {  
        "text":"João Paulo Geroldo",
        "relevance":0.397701
    },
    {  
        "text":"Univem",
        "relevance":0.355912
    },
    {  
        "text":"Univem, Dr. Luiz Carlos",
        "relevance":0.224607
    },
    {  
        "text":"Macedo Soares",
        "relevance":0.221663
    },
    {  
        "text":"São Paulo",
        "relevance":0.193284
    },
    {  
        "text":"Prof. Dr. Fábio Dacêncio Pereira",
        "relevance":0.144916
    },
    {  
        "text":"Prof. Dr. Elvis Fusco",
        "relevance":0.110133
    },
    {  
        "text":"Brasil",
        "relevance":0.0921672
    },
    {  
        "text":"Sevna",
        "relevance":0.0741539
    },
    {  
        "text":"Elvis Fusco",
        "relevance":0.0013093
    }
]

}

猫鼬只订购实体数组,而不订购文件。如何按实体的相关性对检索到的所有文档进行排序?

0 个答案:

没有答案