【发布时间】:2015-11-10 21:26:38
【问题描述】:
我在 MongoDB 上使用 Mongoose。这就是我的模型的外观。
var BookSchema = new Schema({
name: String,
viewCount: { type: Number, default: 0 },
description: {
type: String,
default: 'No description'
},
body: {
type: String,
default: ''
}
}
});
我需要在Name, Description, Body 字段上搜索一些文本。到目前为止,这就是我正在做的及其工作:
Book.find().or([{ 'name': { $regex: term, $options: "$i" }}, { 'description': { $regex: term, $options: "$i" }}, { 'body': { $regex: term, $options: "$i" }}]).exec(
function (err, topics) {
if (err) {
return handleError(res, err);
}
return res.status(200).json(books);
});
问题:我需要想出一些机制来为所有字段(Name,Description,Body)分配权重/分数,其中name 的权重最高,description 的权重少一点比名称和body 的权重最小。当结果出来时,我想按分数/权重对结果进行排序。
到目前为止,我已经研究了这个link 和weights,但不确定获得所需结果的最佳方法是什么。我也想了解,我是否需要在每次搜索之前创建权重,或者它是一次性活动以及如何使用 Mongoose 实现权重?
【问题讨论】:
标签: node.js mongodb mongoose mongodb-query