0

So I am trying to query the following document using $in from Mongoose.

var Reports = [
    {
        "employee" : {
            "gender" : {
                "id": 0,
                "name" : "Male"
            },
            "race" : {
                "id": 1,
                "name" : "White"
            }
        },
        "date" : "2016-12-21T01:03:00.000Z",
        "type": {
                "id": 0,
                "name" : "Business"
         }
    },
    {
        "employee" : {
            "gender" : {
                "id": 1,
                "name" : "Female"
            },
            "race" : {
                "id": 1,
                "name" : "White"
            }
        },
        "date" : "2016-11-02T12:55:00.000Z",
        "type": {
                "id": 1,
                "name" : "Human Resources"
         },
    }
]  

API Route for querying the database

// Load Necessary modules
var mongoose = require('mongoose');
var Report = require("../models/report");

// HTTP POST '/api/query'
router.post('/query', function(req, res, next) {
    var type         = req.body.type;
    var startDate    = req.body.startDate;
    var endDate      = req.body.endDate;
    var gender       = req.body.gender;
    var race         = req.body.race;

    var query = Report.find({});

    if (type){
        query = query.where('type.name').equals(type)
    }
    if (startDate && endDate) {
        query = query
        .where('date')
        .gte(new Date(startDate))
        .lte(new Date(endDate))
    }
    if (gender) {
        // query = query.where('employee.gender.name').in(gender)
        query = query.find({'employee.gender.name': {'$in': gender}})
    }
    if (race) {
        // query = query.where('employee.race.name').in(race)
        query = query.find({'employee.race.name': {'$in': race}})
    }

    query
    .limit(10)
    .exec(function(err,reports){
        if(err)
            return res.send(err);
        res.json(reports)
    })
})  

I should point out that gender and race are arrays of strings (gender = ['Male','Female','Transgender'], race = ['White','Black']) sent by the user in the body of the request.

Setup an Index on the employee property.

db.reports.ensureIndex({employee : 1})  

This is to ensure that queries are more efficient and fast. I even created additional indexes to see if I would make the query work

db.reports.ensureIndex({employee.gender : 1})
db.reports.ensureIndex({employee.gender.name : 1})  

No luck...

The Mongo Shell

When I run this query below in the Mongo Shell, it seems to work, but not when I try using a tool like POSTMAN it doesn't or I try to send the request from the frontend using AngularJS.

> db.reports.find({'suspect.gender.name': {'$in': ['Male']}}).count()
2

In my frontend code I made sure that my headers are set with "Content-Type": "application/x-www-form-urlencoded;charset=utf-8"

Please help. What am I doing wrong here?

sidgate
  • 14,650
  • 11
  • 68
  • 119
AllJs
  • 1,760
  • 4
  • 27
  • 48
  • Did you try to `console.log(gender)`? – michelem Mar 29 '17 at 04:20
  • Or better `console.log(query)` – michelem Mar 29 '17 at 04:21
  • I just did, and I forgot to mention that `console.log(query)` returns this weird object I can't explain `{ '{\n "startDate" : "2016-11-02T12:55:00.000Z",\n "endDate" : "2017-01-02T12:55:00.000Z",\n "race" : [\'Black\', \'White\'],\n "gender" : [\'Female\']\n}': '' }` – AllJs Mar 29 '17 at 04:27

0 Answers0