Monday, 5 June 2017

Mongoose return document if a search string is in an array

Here is a snapshot of my model schema...

mongoose.Promise = Promise;

let postSchema = Schema({
    title: { type: String, unique: true },
    uri: { type: String, unique: true },
    image_uri: { type: String },
    content: {
        mrkdwn: { type: String },
        html: { type: String }
    }, 
    excerpt: { type: String },
    tags: [{ type: String }],
    creator: { type: String, default: 'Shelby Rae Seaton' },
    category: { type: String },
    type: { type: String, default: 'Standard' },
    timestamp: { type: Date, default: Date.now },
    latest_timestamp: { type: Date, default: Date.now }
});

module.exports = mongoose.model('Post', postSchema);

Here is a snapshot of a query...

var queryParams = {
     category: 'someCategory',
     tags: 'someTag'
   }

Post.find(queryParams).exec((err, posts) => {
        // Do some stuff in here
    }

I want to return all results whose tags field contains some 'search string' but it isnt working for me at the moment. How should I go about obtaining this functionality?



via canaan seaton

No comments:

Post a Comment