首页 > 解决方案 > MongoDB Atlas 和 Mongoose - GET API - 需要支持 - 不区分大小写的查询值

问题描述

如果我搜索它,我已经构建了一个 API 来获取返回的餐厅。但是,它仅在大小写相同的情况下才有效。例如,我的 Atlas MongoDB 将 pub 'Shovels Inn' 存储为 'SHOVELS INN'。

所以在 Postman 中,除非我输入 'SHOVELS INN',否则它永远不会找到酒吧,例如 'shovels inn' 无法获得结果。我的数据库中存储了 1024 家餐厅,因此我需要能够输入这些餐厅名称中的任何一个,且不区分大小写。我已经尝试过索引、排序规则、调整我的架构等。我是新手,所以非常感谢任何帮助。

const express = require('express');
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require("mongoose");


const app = express();

app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({
  extended: true
}));
app.use(express.static("public"));

// Make Mongoose use the new unified topology as old one was deprecated
mongoose.set('useUnifiedTopology', true);

// sets an index? Consider removing this
mongoose.set('useCreateIndex', true);

// Connect to MongoDB Atlas

mongoose.connect("mongodb+srv://user:pass@cluster0-fmz1k.mongodb.net/tempDB?retryWrites=true&w=majority", { useNewUrlParser: true});

// Create Schema
const restaurantSchema = mongoose.Schema({
  BusinessName: String,
  BusinessType: String,
  RatingValue: Number
});

// Restaurant Model which uses restaurant schema
const Restaurant = mongoose.model('Restaurant', restaurantSchema);

// Routes - requests targetting all restaurants - returns ALL restaurants//////////////////////////
app.route("/restaurants")

.get(function(req, res){
  Restaurant.find(function(err, foundRestaurants){
    if (!err) {
      res.send(foundRestaurants);
    } else {
      res.send(err);
    }

  });
});

// Route to return a single restaurant using a collection that has collation - still doesnt work//
app.route("/collates/:restaurantName")

.get(function(req, res){
  Restaurant.findOne({BusinessName: req.params.restaurantName}, function(err, foundRestaurant){
    if (foundRestaurant) {
      res.send(foundRestaurant)
    } else {
      res.send("No restaurants matching that title was found.");
    }
  });
});





// Route to return a rating//
app.route("/restaurants/ratings/:restaurantName")

.get(function(req, res){
  Restaurant.findOne({BusinessName: req.params.restaurantName}, {RatingValue: req.params.restaurantName}, function(err, foundRating){
    if (foundRating) {
      res.send(foundRating)
    } else {
      res.send("No restaurant ratings matching that title was found.");
    }
  });
});

// Route to return a Local Authority//
app.route("/restaurants/localauthority/:localauthorityName")

.get(function(req, res){
  Restaurant.find({LocalAuthorityName: req.params.localauthorityName}, function(err, foundAuthority){
    if (foundAuthority) {
      res.send(foundAuthority)
    } else {
      res.send("No local authority matching that input was found.");
    }
  });
});

// If you go to root, then print 'Hello World!'
app.get("/", function(req, res){
  res.send("<h1>Hello World!<h1>");
});

app.listen(3000, function(){
  console.log("Server started on port 3000");
});

标签: mongodbexpressmongoosecollationmongodb-atlas

解决方案


推荐阅读