优化子属性中$ exists的查询 [英] optimizing query for $exists in sub property

查看:35
本文介绍了优化子属性中$ exists的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要搜索另一个对象内是否存在属性. 该集合包含如下文档:

I need to search for the existence of a property that is within another object. the collection contains documents that look like:

"properties": {
    "source": {
        "a/name": 12837,
        "a/different/name": 76129
    }
}

如下所示,查询字符串的一部分来自变量.

As you can see below, part of the query string is from a variable.

在JohnnyHK的帮助下(请参见 mongo查询-属性是否存在?更多信息),我通过执行以下操作获得了一个查询:

With some help from JohnnyHK (see mongo query - does property exist? for more info), I've got a query that works by doing the following:

var name = 'a/name';
var query = {};
query['properties.source.' + name] = {$exists: true};
collection.find(query).toArray(function...

现在,我需要查看是否可以对集合进行索引以提高此查询的性能.

Now I need to see if I can index the collection to improve the performance of this query.

我不知道如何执行此操作,甚至不知道是否可以为此编制索引.

I don't have a clue how to do this or if it is even possible to index for this.

建议?

推荐答案

这里发生了2件事.

首先,您可能正在寻找稀疏索引.

First probably you are looking for sparse indexes.

http://docs.mongodb.org/manual/core/index-sparse /

在您的情况下,它可能是"properties.source.a/name"字段上的稀疏索引.在字段上建立索引将大大缩短查询查找时间.

In your case it could be a sparse index on "properties.source.a/name" field. Making indexes on field will dramatically improve your query lookup time.

db.yourCollectionName.createIndex( { "properties.source.a/name": 1 }, { sparse: true } )

第二件事.总是在您想知道查询是快速还是慢速时,使用mongo console,运行查询及其结果调用explain方法.

Second thing. Always when you want to know whether your query is fast/slow, use mongo console, run your query and on its result call explain method.

db.yourCollectionName.find(query).explain(); 多亏了它,您将知道查询是否使用索引,完成查询必须检查多少文档以及其他一些有用的信息.

db.yourCollectionName.find(query).explain(); Thanks to it you will know whether your query uses indexes or not, how many documents it had to check in order to complete query and some others useful information.

这篇关于优化子属性中$ exists的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆