Mongoose use user input to find results in DB with express.js?

我与影子孤独终老i 提交于 2020-03-05 00:38:21

问题


I'm new to mongoose/mongo and express so I'm not quiet sure how to go about this. So basically I'm trying to use checkboxes to sort of "filter" results in my database.

Here's an example of something in my db:

{ "location" : "PHARMACY",
"region" : "SOCAL",
"system" : "WITS",
"contact" : "SMITH",
"membership" : "TIER1" }

So far I've only figured out how to hard code it like this, where I do Environment.find({region: "SOCAL"}) and it spits out all the db entries that contain SOCAL as a region:

var express = require("express"),
  app = express(),
  mongoose = require("mongoose"),
  bodyParser = require("body-parser");

mongoose.connect("mongodb://localhost/epims", {
  useNewUrlParser: true,
  useUnifiedTopology: true
});
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");

//schema setup
var environmentSchema = new mongoose.Schema({
  name: String,
  region: String,
  system: String,
  contact: String,
  membership: String,
});

var Environment = mongoose.model("Environment", environmentSchema);

app.get("/environments/show", function(req, res) {
  //find environment with selected parameters
  Environment.find({region: "SOCAL"}, function(err, foundEnvironment) {
    if (err) {
      console.log(err);
    } else {
      //render show template with that env
      res.render("show", { environment: foundEnvironment });
    }
  });
});

How do I make it so this search through the database changes based on what selections are made?

Here is my select form if anybody needs to see that as well

<form action="/environments" method="GET">
    <legend>Locations</legend>
    <input type="checkbox" name="location" value="PHARMACY">
    <label for="PHARMACY">PHARMACY</label>

    <input type="checkbox" name="location" value="OFFICE">
    <label for="OFFICE">OFFICE</label>

    <input type="checkbox" name="location" value="STORE">
    <label for="STORE">STORE</label>

    <legend>Regions</legend>
    <input type="checkbox" name="region" value="SOCAL">
    <label for="SOCAL">SOCAL</label>

    <input type="checkbox" name="region" value="NORCAL">
    <label for="NORCAL">NORCAL</label>

    <input type="checkbox" name="region" value="WA">
    <label for="WA">WA</label>

    ...
<a href="/environments/show">Submit</a>
</form>

回答1:


You can pass the search parameters via the search query: /environments/show?location=OFFICE&location=STORE&region=SOCAL&region=NORCAL

The GET request produce this database query:

{ 
  region: { '$in': [ 'SOCAL', 'NORCAL' ] },
  location: { '$in': [ 'OFFICE', 'STORE' ] } 
}

app.get("/environments/show", function (req, res) {

     let region = [req.query.region || []].flat();
let location = [req.query.location || []].flat();


let query = {
    region: {
        "$in": region
    },
    location: {
        "$in": location
    }
};



    Environment.find(query, function (err, foundEnvironment) {
        if (err) {
            console.log(err);
        } else {
            //render show template with that env
            res.render("show", { environment: foundEnvironment });
        }
    });

});
<form action="/environments/show" method="GET">
    <legend>Locations</legend>
    <input type="checkbox" name="location" value="PHARMACY">
    <label for="PHARMACY">PHARMACY</label>

    <input type="checkbox" name="location" value="OFFICE">
    <label for="OFFICE">OFFICE</label>

    <input type="checkbox" name="location" value="STORE">
    <label for="STORE">STORE</label>

    <legend>Regions</legend>
    <input type="checkbox" name="region" value="SOCAL">
    <label for="SOCAL">SOCAL</label>

    <input type="checkbox" name="region" value="NORCAL">
    <label for="NORCAL">NORCAL</label>

    <input type="checkbox" name="region" value="WA">
    <label for="WA">WA</label>

    ...
    <button type="submit">Submit</button>
</form>


来源:https://stackoverflow.com/questions/60400885/mongoose-use-user-input-to-find-results-in-db-with-express-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!