标签: mongodb-indexes

2dsphere vs 2d index:哪个"更好"/更快?

Ι有一个数据库只有我的文件Points.我考虑添加地理空间索引.所以我可以选择2dhere2d.

MongoDB.org有:

2dsphere索引支持:

 - Calculations on a sphere
 - Both GeoJSON objects and legacy coordinate pairs
 - A compound index with scalar index fields (i.e. ascending or
   descending) as a prefix or suffix of the 2dsphere index field
Run Code Online (Sandbox Code Playgroud)

2d索引支持:

 - Calculations using flat geometry 
 - Legacy coordinate pairs (i.e.,    geospatial points on a flat
   coordinate system)  
 - A compound index with    only one additional    field, as a suffix of
   the 2d index field
Run Code Online (Sandbox Code Playgroud)

但是,因为我的所有文档都是积分,所以我可以在我的架构中有一个选项,但没有太大区别. …

mongodb mongodb-indexes

19
推荐指数
1
解决办法
4567
查看次数

mongo中的稀疏索引和空值

我不确定我是否正确理解稀疏索引.

我在fbId上有一个稀疏的唯一索引

{
    "ns" : "mydb.users",
    "key" : {
        "fbId" : 1
    },
    "name" : "fbId_1",
    "unique" : true,
    "sparse" : true,
    "background" : false,
    "v" : 0
}
Run Code Online (Sandbox Code Playgroud)

而且我期望这会允许我插入带有null的记录作为fbId,但是会抛出重复的键异常.它只允许我在完全删除fbId属性时插入.

是不是应该处理的稀疏指数?

indexing mongodb mongodb-indexes

18
推荐指数
1
解决办法
1万
查看次数

Mongodb - poor performance when no results return

I have Mongodb collection with about 7 million documents that represents places.

I run a query that search for places that their name start with a prefix near a specific location.

We have a compound index as described bellow to speed up the search.

When the search query find match (even if only one) the query is execute very fast (~20 milisec). But when there is no match it can take 30 sec for the query to execute.

Please assist. …

mongodb mongodb-indexes

17
推荐指数
1
解决办法
633
查看次数

mongodb:多键索引结构?

我发现很难理解在mongodb中如何在多键上完成索引编制.

这是我在其网站上读到的关于mongodb文档中的多键的内容:
1)"在数组元素索引上创建索引导致数据库索引数组的每个元素"
2)"...将索引文档上的所有标记,并为该文档创建"X","Y"和"Z"的索引条目."

那么该文档的索引条目究竟意味着什么呢?每个文档是否记住条目,在这种情况下,搜索将是一个全表扫描?或者它是与mysql相同的b-tree索引,其中每个索引条目将指向每个相应事件的多个文档,在这种情况下,我过度思考.

我们来举个例子:
obj1 = {
name: "Apollo",
text: "Some text about Apollo moon landings",
tags: [ "moon", "apollo", "spaceflight", "nasa" ]
}

obj2 = {
name: "Atlantis",
text: "Some text about Atlantis flight missions",
tags: [ "space", "atlantis", "spaceflight", "nasa" ]
}

>db.articles.ensureIndex( { tags : 1 } )

请帮我理解!提前致谢.

indexing mongodb mongodb-indexes

15
推荐指数
1
解决办法
4119
查看次数

mongodb在排序时不使用索引?

我有一个这些索引的集合:

> db.message.getIndexKeys()
[
    {
        "_id" : 1
    },
    {
        "msgid" : 1
    },
    {
        "keywords" : 1,
        "msgid" : 1
    }
]
Run Code Online (Sandbox Code Playgroud)

和查询一样

db.message.find({'keywords': {'$all': ['apple', 'banana']}}).limit(30).explain()
Run Code Online (Sandbox Code Playgroud)

索引工作正常

{
    "cursor" : "BtreeCursor keywords_1_msgid_1",    
    "nscanned" : 96,
    "nscannedObjects" : 96,
    ...
}
Run Code Online (Sandbox Code Playgroud)

但在使用msgid进行排序时:

db.message.find({'keywords': {'$all': ['apple', 'banana']}})
    .sort({msgid:-1})
    .limit(30).explain()
Run Code Online (Sandbox Code Playgroud)

mongodb不再使用索引:

{
"cursor" : "BtreeCursor msgid_1 reverse",
"nscanned" : 1784455,
"nscannedObjects" : 1784455,
...
}
Run Code Online (Sandbox Code Playgroud)

任何解决方案

mongodb nosql mongodb-indexes

13
推荐指数
1
解决办法
6941
查看次数

不同类型的MongoDB索引

我们可以在同一个集合中拥有{data:"hello"},{data:123},甚至可以在其上创建索引.我很好奇mongodb如何管理幕后的索引.我们不能在不同类型上创建单个B树.对?但是,我使用getIndexes来查看是否创建了另一个索引,但只创建了一个索引.

indexing mongodb mongodb-indexes

11
推荐指数
1
解决办法
1656
查看次数

MongoDB中唯一索引的优势

我试图搜索Mongo文档,但是无法真正找到有关唯一索引的查询是否比非唯一索引上的查询更快的详细信息(给定相同的数据)

所以我理解一个独特的指数将具有高选择性和良好的性能.但是,如果两个字段的串联是唯一的,那么非唯一复合索引的执行速度是否比唯一复合索引慢?

我假设唯一索引可以减慢插入速度,因为必须验证唯一性.但是,如果有的话,读取性能改进的独特指标真的值得吗?

unique-index mongodb mongodb-indexes

11
推荐指数
1
解决办法
3096
查看次数

了解子文档数组的索引

我一直在研究MongoDB上的数组(多键)索引,我有以下问题,我无法找到很多文档:

子文档数组的索引

所以如果我有一个看起来像这样的数组字段:

{field : [
  {a : "1"}, 
  {b : "2"}, 
  {c : "3"}
  ]
}
Run Code Online (Sandbox Code Playgroud)

我只是在查询field.afield.c独立(不是两者一起),我相信我有以下选项之间进行选择:

  1. db.Collection.ensureIndex({field : 1});
  2. db.Collection.ensureIndex({field.a : 1}); db.Collection.ensureIndex({field.c : 1});

那就是:整个数组的索引; 或嵌入字段上的两个索引.现在我的问题是:

  • 如何在选项1中可视化整个数组的索引(它甚至有用)?这样的索引对哪些查询有用?
  • 鉴于我所描述的查询情况,上述哪两个选项更好,为什么?

mongodb mongodb-indexes

10
推荐指数
1
解决办法
4482
查看次数

mongodb 3.4.2 InvalidIndexSpecificationOption错误:字段'unique'对于_id索引规范无效

该命令db.testCollection.createIndex( { _id: 1 }, {name: "_id_2", unique: true, background: true} )在mongo版本3.4.2上失败,但不是3.2.11.mongo文档表明版本3.4支持uniquebackground属性.

Mongo 3.4.2失败了......

> use testDB
switched to db testDB
> db.testCollection.createIndex( { _id: 1 }, {name: "_id_2", unique: true, background: true} )
{
    "ok" : 0,
    "errmsg" : "The field 'unique' is not valid for an _id index specification. Specification: { ns: \"testDB.testCollection\", v: 1, key: { _id: 1.0 }, name: \"_id_2\", unique: true, background: true }",
    "code" : 197,
    "codeName" : …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb mongodb-indexes

10
推荐指数
1
解决办法
7064
查看次数

mongoDB 与 elasticsearch 查询/聚合性能比较

这个问题是关于为应用程序选择运行查询的数据库类型。暂时将其他因素放在一边,考虑到 mongodb 和 elastic 之间的选择,关键标准是查询应该近乎实时地解决。查询将是临时的,因此可以包含 JSON 对象中的任何字段,并且可能包含聚合和子聚合。此外,不会有嵌套对象,并且没有任何字段将包含“描述性”文本(如电影评论等),即所有字段都将是关键字类型字段,如州、国家/地区、城市、名称等。

现在,我读到 elasticsearch 性能接近实时,并且 elasticsearch 使用倒排索引并为每个字段自动创建它们。综上所述,我的问题如下。(堆栈中发布了一个类似的问题,但我认为它没有回答我的问题 elasticsearch vs MongoDB for filtering application

1)由于我提到的用例中的字段不包含描述性文本,因此不需要弹性提供的全文搜索功能和其他附加功能(特别是对于文本搜索),弹性和弹性之间的更好选择是什么?蒙戈?如果我要在 mongo 中的所有可用字段上创建单个字段索引,弹性搜索和 mongo 查询/聚合性能将如何比较?

2)我不熟悉高级索引,所以我假设可以在 mongo 中的所有可用字段上创建索引(使用多个单字段索引或复合索引?)。我知道这会带来存储和写入速度的成本,这对于弹性也是如此。

3)此外,在弹性中,用户可以在写入速度(索引率)与写入文档变得可用的速度(refresh_interval)之间进行权衡以进行查询。mongo 中是否有类似的功能?

mongodb indices elasticsearch mongodb-indexes elasticsearch-indices

10
推荐指数
1
解决办法
5885
查看次数