- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个 MongoDB 文档集合。我已经为特定字段分配了权重,但我需要将具有任何非空名称 的记录加权到顶部。我不想按名称排序,我只是希望有名称的记录出现在没有名称的记录之前。
示例模式:
new Schema({
slug: {
type: String,
index: {unique: true, dropDups: true}
},
name: String,
body: {
type: String,
required: true
}
});
示例索引:
MySchema.index({
name:'text',
body:'text'
}, {
name: 'best_match_index',
weights: {
name: 10,
body: 1
}
});
查找查询:
MyModel.find( criteria, { score : { $meta: 'textScore' } })
.sort({ score : { $meta : 'textScore' } })
.skip(offset)
.limit(per_page)
最佳答案
如果我在这里理解你的意思,你的意思是给定这样的文档:
{ "name" : "term", "body" : "unrelated" }
{ "name" : "unrelated", "body" : "unrelated" }
{ "body" : "term" }
{ "body" : "term term" }
{ "name" : "unrelated", "body" : "term" }
正常搜索“术语”会产生如下结果:
{ "name" : "term", "body" : "unrelated", "score" : 11 }
{ "body" : "term term", "score" : 1.5 }
{ "body" : "term", "score" : 1.1 }
{ "name" : "unrelated", "body" : "term", "score" : 1.1 }
但您想要的是将最后一个条目作为第二个条目。
为此,您需要另一个字段的“动态”投影以“加权”,您将在其中使用聚合框架:
MyModel.aggregate([
{ "$match": {
"$text": { "$search": "term" }
}},
{ "$project": {
"slug": 1,
"name": 1,
"body": 1,
"textScore": { "$meta": "textScore" },
"nameScore": {
"$cond": [
{ "$ne": [{ "$ifNull": [ "$name", "" ] }, ""] },
1,
0
]
}
}},
{ "$sort": { "nameScore": -1, "textScore": -1 } },
{ "$skip": offset },
{ "$limit": per_page }
],function(err,results) {
if (err) throw err;
console.log( results );
})
将带有“名称”字段的项目放在没有“名称”字段的项目之上:
{ "name" : "term", "body" : "unrelelated", "textScore" : 11, "nameScore" : 1 }
{ "name" : "unrelated", "body" : "term", "textScore" : 1.1, "nameScore" : 1 }
{ "body" : "term term", "textScore" : 1.5, "nameScore" : 0 }
{ "body" : "term", "textScore" : 1.1, "nameScore" : 0 }
本质上是 $ifNull
$cond
内的运算符三元测试是否存在“名称”字段,然后在存在的地方返回 1,在不存在的地方返回 0。
这会传递给 $sort
您的排序在“nameScore”上的管道首先将这些项目 float 到顶部,然后是“textScore”。
聚合管道有自己的 $skip
实现和 $limit
用于分页。
这与 .find()
实现中的操作集基本相同,包括“匹配”、“项目”、“排序”、“跳过”和“限制”。所以这在处理方式上确实没有区别,只是对结果有更多的控制。
“跳过”和“限制”的使用实际上并不是最高效的解决方案,但有时您会坚持使用它,例如在需要提供“页码”的情况下。但是,如果您可以摆脱它并且只需要向前移动,那么您可以尝试将最后一次看到的“textScore”和“seen_ids”列表跟踪到一定的粒度级别,具体取决于“textScore”值的分布方式是。这些可以作为“跳过”结果的替代方法传入:
MyModel.aggregate([
{ "$match": {
"$text": { "$search": "term" }
}},
{ "$project": {
"slug": 1,
"name": 1,
"body": 1,
"textScore": { "$meta": "textScore" },
"nameScore": {
"$cond": [
{ "$ne": [{ "$ifNull": [ "$name", "" ] }, ""] },
1,
0
]
}
}},
{ "$match": {
"_id": { "$nin": seen_ids }
"textScore": { "$gte": last_score },
}},
{ "$sort": { "nameScore": -1, "textScore": -1 } },
{ "$limit": page_size }
])
这里唯一有点不幸的是 $meta
因为 textScore 还不能暴露给初始的 $match
操作,这将有助于缩小结果范围,而无需遍历 $project
首先。
因此,您实际上无法进行与专门的 $geoNear
之类的事情相同的完整 优化。运算符,但它的文本版本或允许前一个语句会很好。
您在这里可能会注意到,从 .aggregate()
选项返回的对象只是原始 JavaScript 对象,而不是从 等操作返回的 Mongoose“文档”对象。查找()
。这是“设计使然”,这里的主要原因是由于聚合框架允许您“操纵”生成的文档,因此这里不能保证这些文档实际上与您最初查询的模式中的文档相同.
由于您并没有真正按照您的预期目的“更改”或“ reshape ”文档,它现在只是退回到您的代码来执行 mongoose 在幕后自动执行的操作并将每个原始结果“转换”为标准的“类型”。
此列表通常应显示您需要执行的操作:
var async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema;
mongoose.connect("mongodb://localhost/test");
var testSchema = new Schema({
name: String,
body: { type: String, required: true },
textScore: Number,
nameScore: Number
},{
toObject: { virtuals: true },
toJSON: { virtuals: true }
});
testSchema.virtual('favourite').get(function() {
return "Fred";
});
var Test = mongoose.model( "Test", testSchema, "textscore" );
Test.aggregate([
{ "$match": {
"$text": { "$search": "term" }
}},
{ "$project": {
"name": 1,
"body": 1,
"textScore": { "$meta": "textScore" },
"nameScore": {
"$cond": [
{ "$ne": [{ "$ifNull": [ "$name", "" ] }, "" ] },
1,
0
]
}
}},
{ "$sort": { "nameScore": -1, "textScore": -1 } },
],function(err,result) {
if (err) throw err;
result = result.map(function(doc) {
return new Test( doc );
});
console.log( JSON.stringify( result, undefined, 4 ));
process.exit();
});
其中包括输出中的“虚拟”字段:
[
{
"_id": "53d1a9b501e1b6c73aed2b52",
"name": "term",
"body": "unrelelated",
"favourite": "Fred",
"id": "53d1a9b501e1b6c73aed2b52"
},
{
"_id": "53d1ae1a01e1b6c73aed2b56",
"name": "unrelated",
"body": "term",
"favourite": "Fred",
"id": "53d1ae1a01e1b6c73aed2b56"
},
{
"_id": "53d1ada301e1b6c73aed2b55",
"body": "term term",
"favourite": "Fred",
"id": "53d1ada301e1b6c73aed2b55"
},
{
"_id": "53d1ad9e01e1b6c73aed2b54",
"body": "term",
"favourite": "Fred",
"id": "53d1ad9e01e1b6c73aed2b54"
}
]
关于node.js - 具有非空字段的 MongoDB/Mongoose 权重记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24946079/
我已经在 kubernetes 中部署了一个 3 pod mongodb statefulset,并且我正在尝试使用新的 mongodb+srv 连接字符串 (mongodb 3.6) 连接到具有 S
我已经创建了 MongoDB Atlas 帐户,并尝试连接。但出现以下错误。 MongoDB 连接错误 MongoNetworkError: 首次连接时无法连接到服务器 [cluster0-shard
我正在使用 Node-WebKit 创建桌面应用程序。该应用程序基本上是创建文档(员工日常工作的详细信息),任何注册用户都可以对这些文档发表评论。我正在创建的文档将被分成几个部分。用户将对特定部分发表
我正在尝试使用官方网站上的安装程序在我的本地机器上安装 mongo DB。但是我不断收到这条消息,有人可以帮忙吗? 我试过提供的解决方案 here但没有帮助。 最佳答案 我建议执行以下操作: 按 Wi
我对 MongoDB 和 MongoDB Compass 非常陌生。 我的客户集合中有大约 1000 条记录。如何通过 MongoDB 指南针一次删除所有记录。 非常感谢, 最佳答案 您可以使用 Mo
当我尝试在我的 Ubuntu 机器中安装 mongodb 时,apt-get 会显示以下选项 mongodb mongodb-clients mongodb-dev mongodb-server 谁能
如何将 Robomongo(或任何其他 mongodb 客户端)连接到由本地 Meteor 应用程序创建的 mongodb 实例? 最佳答案 确保 Meteor 正在本地主机上运行。打开终端窗口并运行
我需要在 MongoDB 中生成一个简单的频率表。假设我在名为 books 的集合中有以下文档。 { "_id": 1, genre: [ "Fantasy", "Crime"
我如何在 mongos mapreduce 中指定一个条件,就像我们在 mongos group 函数中所做的那样。 我的数据是这样的 {lid:1000, age:23}, {lid:3000, a
我的 mongodb 数据库文档中有几个 ID。我需要通过脚本在这些 ID 上创建索引,这样我就不必一次又一次地运行 ensureIndex 命令。 db.getCollection("element
在我的数据库中,每个包含项目的文档中都有一个嵌套的元素数组,格式如下: elements:[ { "elem_id": 12, items: [ {"i_id": 1
我正在构建一个应用程序,其中用户可以位于不同的时区,并且我运行的查询对他们的时区很敏感。 我遇到的问题是 MongoDB 似乎在查询时忽略了时区! 这是日期字段“2019-09-29T23:52:13
我正在研究使用 mongodb 进行分片,我有以下结构: 1 个 Mongod 到我的 ConfigServer,在 ReplicaSet 中只有 1 个成员 2 个分片,每个分片在 ReplicaS
我正在尝试获取一个 mongoDB 对象,例如 Friend1 包含另一个 mongoDB 对象 Friend2,该对象又包含第一个对象 Friend1本质上使它成为一个循环对象引用。 要么这样,要么
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
Mongo 版本 5.0.2。 Ubuntu 20.0 我在本地主机中启用了 MongoDB 连接的安全性。 我正在尝试通过以下命令使用身份验证详细信息连接我的本地主机 MongoDBmongo ad
我即将将分片的 MongoDB 环境从 2.0.7 升级到 2.2.9,最终我想升级到 2.4.9,但显然我需要通过 2.2 来完成。 2.2 的发行说明声明配置服务器应该首先升级其二进制文件,然后是
目前,我无法在我的虚拟 Ubuntu 机器上远程连接 mongodb 服务器。我无法使用在我的 Windows PC 上运行的 Robomongo 客户端连接,该 PC 也运行 vm。 这是两台电脑的
我创建了一个免费的 mongodb 集群。我创建了一个用户,设置了与 mongodb compass 的连接,复制了连接字符串,然后打开了我的 mongodb compass。将复制的字符串粘贴到那里
我使用 java 代码创建了 mongo 数据库集合索引 dbCollection.createIndex("accountNumber"); 当我看到索引使用 db.accounts.getInde
我是一名优秀的程序员,十分优秀!