2016-10-03 64 views
0

我創建mongodb查詢動態,但是當我使用$正則表達式它會引發異常。如果我直接在shell中使用它,查詢就可以工作!

有什麼建議嗎?我真的需要使用動態查詢。

method: this.Collection.Find(query).Limit(limit).ToListAsync(cancellationToken); 

query: { $and: [ { Name: { '$regex': /batman/i } } ] } 

exception:'BsonSerializer.Deserialize<BsonDocument>("{ $and: [ { Name: { '$regex': /batman/i } } ] }")' threw an exception of type 'System.FormatException' 
Data: {System.Collections.ListDictionaryInternal} 
HResult: -2146233033 
HelpLink: null 
InnerException: null 
Message: "JSON reader expected a string but found '/batman/i'." 
Source: "MongoDB.Bson" 

回答

1

您可以嘗試從搜索值中分離選項。

{ $and: [ { Name: { '$regex': 'batman', '$options' : 'i' } } ] }