2013-01-07 39 views
0

我有一個MongoDB實例,我想通過檢查一些正則表達式同時滿足來進行搜索。當我與所有使用$查詢工作/ A /,或者如果我只是使用正則表達式$語法,而不是當我同時使用:

> db.claims.findOne({'tags': {$all: [/a/]}}, {description: 1, tags: 1}) 
{ "_id" : ..., "description" : "Test claim", "tags" : [ "general", "foobar" ] } 
> db.claims.find({'tags': {$regex: 'a'}}, {description: 1, tags: 1}) 
{ "_id" : ..., "description" : "Test claim", "tags" : [ "general", "foobar" ] } 
> db.claims.find({'tags': {$all: [{'$regex': 'a'}]}}, {description: 1, tags: 1}) 
> 

這是怎麼回事?當這兩者一起使用時有沒有錯誤?

回答

2

如果我理解正確,使用$and應該做的伎倆。

db.claims.find({"$and" : [{"tags":{ $regex : 'a'}},{"tags":{ $regex : 'a'}}]}, {description: 1, tags: 1}) 

如果只有一個正則表達式需要滿足使用$或。當然你可以混合搭配$or$and