我正在try 获取包含我传递给筛选器的URL的文档,这些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;

我希望它只返回那些在其标题或在其任务字段中包含上午的文档.

推荐答案

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

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

Node.js相关问答推荐

需要关于基于角色授权的设计建议

node 模块错误:类型参数OT具有循环约束''

如何在.npmrc中添加 comments ?

动态设置元数据,无需重复请求 NextJS 13

仅在 vue 脚本未退出的情况下使用 docker 时出现错误

mongoose findOneAndUpdate 不更新数据库

无服务器部署使用无服务器组合抛出`spawn serverless ENOENT`

如何在没有云功能的情况下使用 Google PubSub

WSL2 上需要脚本运行的 NPM 包的权限被拒绝

如何在 cypress 测试中进行计算

当我们有esnext时,为什么我们需要nodenext typescript 编译器选项?

file.slim.js 中的苗条是什么

mongoose 模式中的嵌套对象

Webpack TypeScript module.hot 不存在

AngularJS +sails.js

代理后面的凉亭

node.js 中存储的模块变量在什么范围内?

如何在离线时安装 npm 包?

在 Node.js 中使用公钥加密数据

Heroku + Node:找不到模块错误