2013-04-03 50 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); 

此代碼將提供所有的輸出響應女巫沒有學校大學話。它對我來說是完美的。

相關問題