简体   繁体   中英

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.

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);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM