【问题标题】:Searching in mongodb atlas on express在 express 上搜索 mongodb atlas
【发布时间】:2021-08-21 21:57:44
【问题描述】:

大家好,我有关于 mongodB atlas 的数据,这是我的 mongoose Schema mongoose Schema 这是客户 client

import React, { useState, useEffect } from 'react';
import { Container, AppBar, Typography, Grow, Grid } from '@material-ui/core';
import { useDispatch } from 'react-redux';

import SearchBar from "material-ui-search-bar";

import Posts from './components/Posts/Posts';
import Form from './components/Form/Form';
import { getPosts } from './actions/posts';
import useStyles from './styles';
import memories from './images/memories.png';

const App = () => {
  const [currentId, setCurrentId] = useState(0);
  const dispatch = useDispatch();
  const classes = useStyles();

  useEffect(() => {
    dispatch(getPosts());
  }, [currentId, dispatch]);

  return (
    <Container maxWidth="lg" >
      <AppBar className={classes.appBar} position="static" color="inherit">
        <Typography className={classes.heading} variant="h2" align="center">Search Bar</Typography>

        <SearchBar  />

      </AppBar>
      <Grow in>
        <Container>
          <Grid container justify="space-between" alignItems="stretch" spacing={3}>
            <Grid item xs={12} sm={7}>
              <Posts setCurrentId={setCurrentId} />
            </Grid>
            <Grid item xs={12} sm={4} >
              <Form currentId={currentId} setCurrentId={setCurrentId} />
            </Grid>
          </Grid>
        </Container>
      </Grow>
    </Container>
  );
};

export default App;

使用这个https://github.com/TeamWertarbyte/material-ui-search-bar

搜索我尝试使用“猫鼬模糊搜索插件” 我这样做Mongoose Fuzzy Searching

import mongoose from 'mongoose';
import mongoose_fuzzy_searching from 'mongoose-fuzzy-searching';

const postSchema = mongoose.Schema({
    title: String,
    message: String,
    creator: String,
    tags: [String],
    selectedFile: String,
    link:String,
    createdAt: {
        type: Date,
        default: new Date(),
    },
})
postSchema.plugin(mongoose_fuzzy_searching, { fields: ['message', 'title'] });

var PostMessage = mongoose.model('PostMessage', postSchema);

export default PostMessage;
我需要在路由器中写什么?我不明白... 以及我如何在反应中显示结果? 另外,我如何进行 URL 验证?

【问题讨论】:

  • 堆栈溢出 (SO) 允许在帖子中嵌入代码。与其提供代码图片的链接,不如直接将实际代码粘贴到帖子中,这样想要帮助的人就不必重新输入示例代码来进行测试。
  • 谢谢老哥搞定了

标签: node.js mongodb express mongoose


【解决方案1】:

您必须像这样在后端 API 中使用模糊搜索

await Product.fuzzySearch(search).find()

【讨论】:

  • 我这样做我放下
【解决方案2】:

export const SearchPost = async (req, res) => {

    const search =  req.body.search;

    try {
        const search = await PostMessage.fuzzySearch(search).find();

        res.status(200).json(search);
    } catch (error) {
        res.status(404).json({ message: error.message });
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 2021-12-10
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2021-06-01
    • 2021-04-26
    • 1970-01-01
    相关资源
    最近更新 更多