猫鼬文本索引搜索返回空数组

时间:2020-09-14 12:28:59

标签: mongodb express mongoose

我正在尝试查询索引,但是收到一个空数组。我找不到我的代码出了什么问题。我使用了两种方法来创建索引:1)VideoSchema.index()和2)在模式本身中,这两种方法都不起作用。我检查了mongodb,看来索引创建正确,所以我不知道我做错了什么。

    const mongoose = require("mongoose");

    const VideoSchema = mongoose.Schema(
        {
            user: {
                type: mongoose.ObjectId,
                required: true,
                ref: "user",
            },
            title: {
                type: String,
                maxLength: 100,
                text: true,
            },
            description: {
                type: String,
                text: true,
            },
            publishDate: {
                type: Date,
            },
            views: {
                type: Number,
                default: 0,
            },
            likes: {
                type: Number,
                default: 0,
            },
            dislikes: {
                type: Number,
                default: 0,
            },
            comments: [
                {
                    type: mongoose.ObjectId,
                    ref: "comment",
                },
            ],
            urls: {
                video_url: {
                    type: String,
                    required: true,
                },
                thumbnail_url: {
                    type: String,
                },
                preview_url: {
                    type: String,
                    required: true,
                },
            },
            private: {
                type: Boolean,
                default: 0,
            },
            category: {
                type: String,
                default: "",
            },
            duration: {
                type: Number,
                required: true,
            },
        },
        { timestamps: true }
    );
    
    // VideoSchema.index({ title: "text", description: "text" });
    
    // export model user with UserSchema
    module.exports = mongoose.model("video", VideoSchema);

查询:

    const express = require("express");
    const router = express.Router();
    const Video = require("../model/Video");
    
    router.post("/", (req, res) => {
        const query = req.body.query;
        Video.find({ $text: { $search: query } }, { score: { $meta: "textScore" } })
            .sort({ score: { $meta: "textScore" } })
            .exec(function (error, results) {
                if (error) return res.status(400).send(error);
                res.status(200).json({ results });
            });
    });
    
    module.exports = router;

MongoDB indexes

2 个答案:

答案 0 :(得分:1)

当您从数据库中获取数据时,这是一个好习惯,并且如果使用“ GET”方法,可使代码更清晰。如果这样做,则自V.4.4起无需在查询中添加得分选项

const express = require("express");
const router = express.Router();
const Video = require("../model/Video");

router.get("/", (req, res) => {
    const query = req.query.YOUR_QUERY_PARAMETER;
    Video.find({ $text: { $search: query }})
        .sort({ score: { $meta: "textScore" } })
        .exec(function (error, results) {
            if (error) return res.status(400).send(error);
            res.status(200).json({ results });
        });
});

module.exports = router;

如果问题仍然存在: 尝试添加通配符文本索引,以查看问题是否在其中,如下所示:

VideoSchema.index({'$**': 'text'});

如果是这样,则删除该集合以重新开始建立索引,然后像这样附加文本索引:

VideoSchema.index({ title: "text", description: "text" });

创建新的虚拟物品,然后再次检查。

确保您阅读了MongoDB文档中显示的异常: https://docs.mongodb.com/manual/reference/operator/query/text/

答案 1 :(得分:0)

看来我已经解决了问题。我注意到在express js中,“查询”关键字用于“获取”请求参数,因此我决定将此变量更改为“搜索”,所以现在就像在下面一样,并且可以正常工作!

router.get("/", (req, res) => {
    const { search } = req.query;
    Video.find(
        { $text: { $search: search } },
        { score: { $meta: "textScore" } }
    )
        .sort({ score: { $meta: "textScore" } })
        .exec(function (error, results) {
            if (error) return res.status(400).send(error);
            res.status(200).json({ results });
        });
});

但是我注意到我只得到一个视频,而不是标题中包含“ obs”的两个视频,所以现在我需要处理它。

非常感谢您的时间和精力!