mongodb 为什么$or不能在mongoose中使用正则表达式正常工作

pgky5nke  于 2022-12-03  发布在  Go
关注(0)|答案(1)|浏览(120)

我正试图获取包含任何我传递在过滤器的url必须出现在标题字段或在这些文档的任务数组:
示例:localhost:4000/search?filter=Morning.在这里我查找标题或任务中包含Morning得文档.
但当我点击API时,它会返回所有文档。

{
    "success": true,
    "message": "Successfully fetched todos",
    "filteredArray": [
        {
            "_id": "6386e34e2c65763cf25008f3",
            "toDoTitle": {
                "title": "Morning routines",
                "_id": "6386e34e2c65763cf25008f4",
                "createdAt": "2022-11-30T04:59:58.940Z",
                "updatedAt": "2022-11-30T04:59:58.940Z"
            },
            "toDoTasks": {
                "tasks": [
                    "code",
                    "sleep",
                    "code",
                    "eat",
                    "code"
                ],
                "_id": "6386e34e2c65763cf25008f5",
                "createdAt": "2022-11-30T04:59:58.941Z",
                "updatedAt": "2022-11-30T04:59:58.941Z"
            },
            "createdAt": "2022-11-30T04:59:58.941Z",
            "updatedAt": "2022-11-30T04:59:58.941Z",
            "__v": 0
        },
        {
            "_id": "6386e3552c65763cf25008f7",
            "toDoTitle": {
                "title": "Morning",
                "_id": "6386e3552c65763cf25008f8",
                "createdAt": "2022-11-30T05:00:05.813Z",
                "updatedAt": "2022-11-30T05:00:05.813Z"
            },
            "toDoTasks": {
                "tasks": [
                    "code",
                    "sleep",
                    "code",
                    "eat",
                    "code"
                ],
                "_id": "6386e3552c65763cf25008f9",
                "createdAt": "2022-11-30T05:00:05.813Z",
                "updatedAt": "2022-11-30T05:00:05.813Z"
            },
            "createdAt": "2022-11-30T05:00:05.813Z",
            "updatedAt": "2022-11-30T05:00:05.813Z",
            "__v": 0
        },
        {
            "_id": "6386e35f2c65763cf25008fb",
            "toDoTitle": {
                "title": "evening",
                "_id": "6386e35f2c65763cf25008fc",
                "createdAt": "2022-11-30T05:00:15.809Z",
                "updatedAt": "2022-11-30T05:00:15.809Z"
            },
            "toDoTasks": {
                "tasks": [
                    "code",
                    "sleep",
                    "code",
                    "eat",
                    "code"
                ],
                "_id": "6386e35f2c65763cf25008fd",
                "createdAt": "2022-11-30T05:00:15.809Z",
                "updatedAt": "2022-11-30T05:00:15.809Z"
            },
            "createdAt": "2022-11-30T05:00:15.810Z",
            "updatedAt": "2022-11-30T05:00:15.810Z",
            "__v": 0
        },
        {
            "_id": "6386e3672c65763cf25008ff",
            "toDoTitle": {
                "title": "evening",
                "_id": "6386e3672c65763cf2500900",
                "createdAt": "2022-11-30T05:00:23.977Z",
                "updatedAt": "2022-11-30T05:00:23.977Z"
            },
            "toDoTasks": {
                "tasks": [
                    "code"
                ],
                "_id": "6386e3672c65763cf2500901",
                "createdAt": "2022-11-30T05:00:23.977Z",
                "updatedAt": "2022-11-30T05:00:23.977Z"
            },
            "createdAt": "2022-11-30T05:00:23.977Z",
            "updatedAt": "2022-11-30T05:00:23.977Z",
            "__v": 0
        },
        {
            "_id": "6386e3712c65763cf2500903",
            "toDoTitle": {
                "title": "night",
                "_id": "6386e3712c65763cf2500904",
                "createdAt": "2022-11-30T05:00:33.286Z",
                "updatedAt": "2022-11-30T05:00:33.286Z"
            },
            "toDoTasks": {
                "tasks": [
                    "code"
                ],
                "_id": "6386e3712c65763cf2500905",
                "createdAt": "2022-11-30T05:00:33.286Z",
                "updatedAt": "2022-11-30T05:00:33.286Z"
            },
            "createdAt": "2022-11-30T05:00:33.287Z",
            "updatedAt": "2022-11-30T05:00:33.287Z",
            "__v": 0
        }
    ]
}

这是我的模特

const mongoose = require("mongoose");
const schema = mongoose.Schema;

const title = new schema(
    {
       title:{ 
        type: String,
        trim: true
        }

    },
    {
        timestamps: true 
    }

    );

const task = new schema(
    {
    tasks:[{ type: String,trim: true}]
    },
    {
    timestamps: true 
    }
    )   

const toDoSchema = new schema({
    toDoTitle : title,
    toDoTasks: task

    },
    {
    timestamps: true 
    });

const toDoModel = mongoose.model("toDo", toDoSchema);
module.exports = toDoModel;

这是我的过滤控制器:

//importing model
const toDoModel = require('../models/ToDoModel');

//importing utils function
const toDosPerPage = require("../utlis/ToDosPerPage")

const searchController = async (req,res)=>{

    try{
        const {filter} = req.query
        if(!filter) throw new Error("Please provide Something in filter to search");
        const filteredArray = await toDoModel.find({
            $or:[{title:new RegExp(filter,'i')},{tasks: new RegExp(filter,'i')}]
        });

        
        
        res.status(200).json({
            success: true,
            message: "Successfully fetched todos",
            filteredArray

        });

        }
        catch(err){
            res.status(401).json({
                success: false,
                message: "Some error occuried",
                error: err.message
            })
            console.log(err.message);
        } 
    }
    module.exports = searchController;

我希望它只返回那些标题或任务字段中包含早晨的文档。

fruv7luv

fruv7luv1#

要在嵌入文档中查找,请使用dot .

const filteredArray = await toDoModel.find({
   $or:[
        {'toDoTitle.title':new RegExp(filter,'i')},
        {'toDoTasks.tasks':new RegExp(filter,'i')}
   ]
});

相关问题