Emily
Emily

Reputation: 391

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

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>

Upvotes: 1

Views: 1674

Answers (1)

Marc
Marc

Reputation: 3904

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>

Upvotes: 2

Related Questions