Handling undefined variables within find() queries

268 views
Skip to first unread message

micros...@gmail.com

unread,
Oct 24, 2017, 3:52:59 PM10/24/17
to Mongoose Node.JS ODM
Hi!

Very new user to both javascript and mongoose, but I have managed to get a database and website to access it up and running with minimal hassle.  The first real problem I have run into is handling user input to search for examples of the following model:

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var PaperSchema = Schema({
  title
: {type: String, required: true},
  person
: {type: Schema.ObjectId, ref: 'Person', required: true},
  comment
: {type: String},
  field
: [{type: Schema.ObjectId, ref: 'Field'}],
  topic
: [{type: Schema.ObjectId, ref: 'Topic'}],
  species
: [{type: Schema.ObjectId, ref: 'Species'}],
  journal
: {type: Schema.ObjectId, ref: 'Journal', required: true}
 
});


The query webpage has a field for each type of entry (technically a set of checkboxes, one for each species, etc.), but if a user only wanted all papers with species = 'mouse' and 'human', all of the other fields would be left blank (or unchecked).
My first pass at handling the query was just the following after getting the query object:

Paper.find({topic:query.topic, field:query.field, species:query.species, journal:query.journal, person:query.person}).exec(function(err, papers){

            res
.render('query_detail', { title: 'Results for query:  ', query:query, papers:papers} );
 
})


which fails to return anything most of the time, because all of the papers it is searching actually have entries for all variables.  This, unfortunately, will only return Paper objects that are mostly blank!

The question:
Is it possible to handle the undefined or empty fields within the find statement?
Or is this the sort of thing where I need the conditional logic outside the find() structure, say an if statement for each variable followed by it's own search, followed by anding all of the sets of results?

if (query.topic){Paper.find({topic:query.topic}, callback here)}
//a lot of code to combine all of the results for this and species, person, etc.

Roughly.

I appreciate any replies, and please let me know if anything can be made more clear, or more code should be provided!

micros...@gmail.com

unread,
Oct 24, 2017, 5:33:26 PM10/24/17
to Mongoose Node.JS ODM
Ended up building a new js object and using that as my query with a mass of if statements.  It works, but if there is a way within find() I would love to know!

        var q = {}
       
if (query.person.length){q.person = query.person;}
       
if (query.topic.length){q.topic = query.topic;}
       
if (query.field.length){q.field = query.field;}
       
if (query.species.length){q.species = query.species;}
       
if (query.journal.length){q.journal = query.journal;}
       
if (query.conference.length){q.conference = query.conference;}
       
if (query.title){q.title = query.title;}
       
if (query.comment){q.comment = query.comment; }
       
Paper.find(q).exec(function(err2, papers) {
Reply all
Reply to author
Forward
0 new messages