如何在猫鼬中精确获取所有价值 [英] How to seach all value nearly exact in mongoose
问题描述
当我使用userInfos.find({username:"John ABC"})
时,我需要让所有人都出现约翰"或ABC事件.
When I use userInfos.find({username:"John ABC"})
I need to get all people with "John" or ABC occurence.
这是我的代码:
const userSchema = new mongoose.Schema({
username:String,
password:String,
});
const userInfos = mongoose.model('userInfos', userSchema);
什么是最好的方法
推荐答案
您可以使用文本搜索.
$text
将使用空格和大多数标点符号作为分隔符来标记搜索字符串,并对搜索字符串中的所有此类标记执行逻辑或.
$text
will tokenize the search string using whitespace and most punctuation as delimiters, and perform a logical OR of all such tokens in the search string.
首先,您需要为用户名字段创建文本索引这个:
First you need to create a text index for username field like this:
db.users.createIndex( { username: "text" } )
然后,您可以使用以下代码搜索猫鼬:
Then you can use the following code to search with mongoose:
const express = require("express");
const router = express.Router();
const User = require("../models/user");
router.get("/search", async (req, res) => {
const result = await User.find({ $text: { $search: "John ABC" } }).select(
"username"
);
res.send(result);
});
假设我们有5个这样的用户:
Let's say we have 5 users like this:
[
{
"_id": "ObjectId(\"5dea9bf903d65c440c1f169e\")",
"username": "Jonas"
},
{
"_id": ObjectId("5dea9bee03d65c440c1f169d"),
"username": "ABCD"
},
{
"_id": ObjectId("5dea9be103d65c440c1f169c"),
"username": "John ABC"
},
{
"_id": ObjectId("5dea9bd503d65c440c1f169b"),
"username": "ABC",
},
{
"_id": ObjectId("5dea9bcd03d65c440c1f169a"),
"username": "John"
}
]
搜索将返回这3个用户:
Search will return these 3 users:
[
{
"_id": "5dea9bd503d65c440c1f169b",
"username": "ABC"
},
{
"_id": "5dea9be103d65c440c1f169c",
"username": "John ABC"
},
{
"_id": "5dea9bcd03d65c440c1f169a",
"username": "John"
}
]
这清楚地表明搜索工作正常.
This shows clearly that the search worked clearly.
如果需要,您甚至可以添加文本分数并按分数排序:
You can even add text score and sort by score if you want like this:
router.get("/search", async (req, res) => {
const result = await User.find(
{ $text: { $search: "John ABC" } },
{ score: { $meta: "textScore" } }
)
.sort({ score: { $meta: "textScore" } })
.select("username");
res.send(result);
});
结果将具有这样的得分:
The result will have score like this:
[
{
"_id": "5dea9be103d65c440c1f169c",
"username": "John ABC",
"score": 1.5
},
{
"_id": "5dea9bcd03d65c440c1f169a",
"username": "John",
"score": 1.1
},
{
"_id": "5dea9bd503d65c440c1f169b",
"username": "ABC",
"score": 1.1
}
]
如您所见,我们在输出中有一个score字段,并且用户按score排序,这非常有用.
As you see, we have score field in the output, and users are sorted by score which is very useful.
如果您使用本地MongoDB,并且在创建文本索引时遇到困难,则可以使用
If you use local MongoDB and if you have difficulty creating the text index, you can use mongo shell.
- 在安装了MongoDB服务器的bin文件夹中打开命令.例如
C:\Program Files\MongoDB\Server\4.0\bin
- 键入
mongo
- 键入
db
- 使用您的数据库名称,假设它是auth,键入
use auth
- 键入
db.users.createIndex( { username: "text" } )
- Open a command in the bin folder you installed MongoDB server. For example
C:\Program Files\MongoDB\Server\4.0\bin
- type
mongo
- type
db
- Using your db name, let's say it is auth, type
use auth
- type
db.users.createIndex( { username: "text" } )
如果使用MongoDB Atlas,则可以这样创建文本索引:
And if you use MongoDB Atlas, you can create the text index like this:
- 连接到MongoDB地图集
- 选择您的数据库并收集
- 在索引"标签中,点击创建索引"按钮
- 像这样创建索引:
- Connect to the MongoDB atlas
- Select your database, and collection
- In the Indexes tab, click CREATE INDEX button
- Create you index like this:
这篇关于如何在猫鼬中精确获取所有价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!