Ι有一个数据库只有我的文件Points.我考虑添加地理空间索引.所以我可以选择2dhere和2d.
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)
但是,因为我的所有文档都是积分,所以我可以在我的架构中有一个选项,但没有太大区别. …
我不确定我是否正确理解稀疏索引.
我在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属性时插入.
是不是应该处理的稀疏指数?
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文档中的多键的内容:
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 } )
请帮我理解!提前致谢.
我有一个这些索引的集合:
> 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)
任何解决方案
我们可以在同一个集合中拥有{data:"hello"},{data:123},甚至可以在其上创建索引.我很好奇mongodb如何管理幕后的索引.我们不能在不同类型上创建单个B树.对?但是,我使用getIndexes来查看是否创建了另一个索引,但只创建了一个索引.
我试图搜索Mongo文档,但是无法真正找到有关唯一索引的查询是否比非唯一索引上的查询更快的详细信息(给定相同的数据)
所以我理解一个独特的指数将具有高选择性和良好的性能.但是,如果两个字段的串联是唯一的,那么非唯一复合索引的执行速度是否比唯一复合索引慢?
我假设唯一索引可以减慢插入速度,因为必须验证唯一性.但是,如果有的话,读取性能改进的独特指标真的值得吗?
我一直在研究MongoDB上的数组(多键)索引,我有以下问题,我无法找到很多文档:
子文档数组的索引
所以如果我有一个看起来像这样的数组字段:
{field : [
{a : "1"},
{b : "2"},
{c : "3"}
]
}
Run Code Online (Sandbox Code Playgroud)
我只是在查询field.a和field.c独立(不是两者一起),我相信我有以下选项之间进行选择:
db.Collection.ensureIndex({field : 1});db.Collection.ensureIndex({field.a : 1});
db.Collection.ensureIndex({field.c : 1});那就是:整个数组的索引; 或嵌入字段上的两个索引.现在我的问题是:
该命令db.testCollection.createIndex( { _id: 1 }, {name: "_id_2", unique: true, background: true} )在mongo版本3.4.2上失败,但不是3.2.11.mongo文档表明版本3.4支持unique和background属性.
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) 这个问题是关于为应用程序选择运行查询的数据库类型。暂时将其他因素放在一边,考虑到 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