2013-04-03 52 views
0

我是Mongo Db的新手,并试图编写一些查询。如何在多个字符串上查询mongo集合

我需要提取其品牌值不等于'any'或'none'或包含'not'的商品列表。

我已经尝试了一些行,但它不工作。

var query = { "BarCode": { "$exists": 1,"$not":"any" }}; 

但它是给出了很少的错误。有人能帮助我吗?

感谢,

纳雷什

+0

你会得到哪些错误? –

+0

@AndreasSchlapsi,错误与'$ not'有关。而且我不确定如何在查询中包含多个字符串来检查不等式。谢谢。 – Naresh

回答

5

使用$ NE运营商 “不等于”(http://docs.mongodb.org/manual/reference/operator/ne/#op._S_ne):

var query = { "BarCode": { "$exists": 1, "$ne": "any" }}; 

所以完整的查询看起来是这样的:

var query = { "Barcode": { "$exists": 1, "$nin": [ "any", "none"], "$not": /not/ }} 

$ nin运算符(“不在”)选择字段值不是指定值之一的文档(请参阅http://docs.mongodb.org/manual/reference/operator/nin/#op._S_nin)。 $ not操作符在正则表达式运算符上执行逻辑NOT(请参阅http://docs.mongodb.org/manual/reference/operator/regex/#op._S_regex)。如果该字段不应包含“不”字,请使用更复​​杂的正则表达式。请记住,此查询不会使用任何索引,因为正则表达式不以^开头。

+0

感谢您的回复。如何在条码中包含'any'和'none'并且不包含'not'。 – Naresh

+0

使用$ nin http://docs.mongodb.org/manual/reference/operator/nin/ – Devesh

0

如果您想对多个字符串进行RegEx匹配,请使用Below Code。

$matchStr = array(new MongoRegex('/school/i'), new MongoRegex('/college/i')); 

$whereCondition = array(
    'name' => array('$nin' => $matchStr), 
    'name' => array('$exists' => 1) 
    ); 

$response = $this->Collection->find($whereCondition); 

此代码将提供所有的输出响应女巫没有学校大学话。它对我来说是完美的。

相关问题