给出一个mongoose 物体.如何在Document Object中执行全文搜索
{
"product" :[
{
"_id": "633d32c4ea9b06177e183388",
"document": {
"_id": "633d32c4ea9b06177e183388",
"category": [
"62bacb137e6e0b6ac850b9ce",
"633d19260b72ba127d928e74"
],
"name": "Iphone 13",
"slug": "iphone-13",
"__v": 0,
"price": 2000.5,
"images": [
{
"_id": "63cca1a271c3635246eee937",
"url": "https://res.cloudinary.com/..."
}
],
"sold": 15,
"ratings": []
},
"avgRating": null
},
...
]
}
这就是我试过的方法,但不起作用
const product = await Product.aggregate([
{
$project: {
document: "$$ROOT",
avgRating: {
$floor: { $avg: "$ratings.star" },
},
},
},
{
$match: {
document: { $text: { $search: `"${searchText}"` } },
},
},
]);
这是我的产品型号
const product = {
name: {
type: String,
required: true,
trim: true,
minlength: [2, "Too short"],
maxlength: [32, "Too long"],
unique: true,
},
slug: {
type: String,
unique: true,
lowercase: true,
index: true,
},
category: [
{
type: ObjectId,
ref: "Category",
},
],
description: {
type: String,
maxlength: 200,
},
price: {
type: Number,
required: true,
trim: true,
maxlength: 32,
},
shipping: {
type: String,
enum: ["Yes", "No"],
},
color: [
{
type: String,
enum: ["Black", "Brown", "Silver", "White", "Blue"],
},
],
sold: {
type: Number,
default: 0,
},
quantity: {
type: Number,
},
images: [
{
public_id: String,
url: String,
},
],
ratings: [
{
star: Number,
postedBy: {
type: ObjectId,
ref: "User",
},
},
],
};
我们的目标是通过获取产品评级的平均值来汇总产品评级,然后链接多个WHERE子句,其中一个WHERE子句是在文档对象中执行全文搜索.
敬请指教.
谢谢.