0

我有以下代码用于在 MongoDB 中检索消息,这些消息恰好有一个按位标志“状态”,这就是为什么我使用内置的 BsonJavaScript 功能来过滤这些记录。该语句是否会尝试在 ToUserId 或 Status 字段上使用索引,即使它使用 JavaScript 语法进行查询评估?

public long NewMessageCount
{
    get
    {
        var script = CreateScript("Status", MessageStatus.Unread);
        return MongoConnectionHandler.MongoCollection.Count(Query.Where(script));    
    }
}

public string ToUserId = "51e8dd21d84513129c644fa6";
public string CreateScript(string field, MessageStatus filter)
{
    return String.Format("this.ToUserId == '{0}' && (this.{1} & {2}) == {3}", ToUserId, field, (int)filter);
}
4

1 回答 1

1

根据$whereMongoDB 2.4 的文档:

$where evaluates JavaScript and cannot take advantage of indexes.

我鼓励您在 MongoDB 问题跟踪器中观看/支持SERVER-3518,这是对按位查询运算符的功能请求。

除了将标志复制到不需要按位查询或使用低效$where查询的布尔字段中之外,在此期间实际上并没有很好的解决方法。

于 2013-07-20T01:10:12.640 回答