Anna
Anna

Reputation: 1003

Fetch entries from mongodb using mongoose

I am using mongoose to operate mongodb in node. And now I have to query entries from Post table where tags doesn't contain any tag like inc:___, inc:gdc, exc:abc, exc:57uyht7, all others tags are allowed like(test,card,check).

PostModel.Post.find({
$where:this.activeFlag==true && (this.tags!= null && this.tags != /^inc:/ && this.tags !=/^exc:/)
}), function(err, stack) {
        if (!err) {
            logger.debug('Posts found successfully.');
        } else {
            logger.error('Problem in finding posts, error :' + err);
        }
    });

But this is not working.. My query fetches entries with inc:dbgh also. Can anyone help me? Thanks in advance.

Upvotes: 0

Views: 183

Answers (1)

Sergey Lapin
Sergey Lapin

Reputation: 2693

According to Mongo docs, you should pass either a string or javascript function to $where, and you pass javascript expression, which gets evaluated in place.

Also, your query can be simplified to

PostModel.Post.find({
    activeFlag: true,
    tags: {
      $exists: true,
      $not: /^(inc:|ecx:)/
    }
}, function(err, stack) {
    if (!err) {
        logger.debug('Posts found successfully.');
    } else {
        logger.error('Problem in finding posts, error :' + err);
    }
});

Upvotes: 1

Related Questions