繁体   English   中英

使用mongoose从mongodb获取条目

[英]Fetch entries from mongodb using mongoose

我正在使用猫鼬在节点中操作mongodb。 现在,我必须从Post表中查询条目,其中的标签不包含任何标签,例如inc:___,inc:gdc,exc:abc,exc:57uyht7,所有其他标签都可以使用(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);
        }
    });

但这是行不通的。我的查询也使用inc:dbgh获取条目。 谁能帮我? 提前致谢。

根据Mongo docs ,您应该将字符串或javascript函数传递给$where ,然后传递javascript表达式,该表达式将在适当位置进行评估。

此外,您的查询可以简化为

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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