有没有办法在shell中的mongodb中查看集合中的索引列表?我通过http://www.mongodb.org/display/DOCS/Indexes阅读,但我什么也看不见
使用代码:
all_reviews = db_handle.find().sort('reviewDate', pymongo.ASCENDING)
print all_reviews.count()
print all_reviews[0]
print all_reviews[2000000]
Run Code Online (Sandbox Code Playgroud)
计数打印2043484,然后打印all_reviews[0].
但是在打印时all_reviews[2000000],我收到错误:
pymongo.errors.OperationFailure:数据库错误:运行程序错误:溢出排序阶段缓冲数据使用量33554495字节超过内部限制33554432字节
我该如何处理?
我正在实现一个处理很多字段的联系人数据库.它们中的大多数是预定义的,可以被认为是绑定的,但有一些不是.我们会称其中一个字段为"群组".我们目前实现它的方式是(每个文档/联系人都有'groups'字段):
'groups' : {
152 : 'hi',
111 : 'group2'
}
Run Code Online (Sandbox Code Playgroud)
但经过一些阅读后,我觉得我应该这样做:
'groups' : [
{ 'id' : 152, 'name' : 'hi' },
{ 'id' : 111, 'name' : 'group2' }
...
]
Run Code Online (Sandbox Code Playgroud)
然后应用索引 db.contact.ensureIndex({'groups.id':1});
我的问题是关于功能.两个结构之间有什么区别?如何实际构建索引(它只是在每个文档/联系人中编制索引,还是构建一个包含所有文档/联系人的所有组的完整索引?).
我有点假设这是结构上最好的方式,但如果我不对,请告诉我.
在mongodb中有多种类型的索引.对于这个问题,我对可用于排序的升序(或降序)索引以及根据文档"主要与分片集群一起使用以支持散列分片键" 的哈希索引感兴趣"(源)确保"更多均匀分布数据"(来源)
我知道你不能创建一个索引,db.test.ensureIndex( { "key": "hashed", "sortOrder": 1 } )因为你得到一个错误
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"errmsg" : "exception: Currently only single field hashed index supported.",
"code" : 16763,
"ok" : 0
}
Run Code Online (Sandbox Code Playgroud)
我的问题:
在指数之间:
db.test.ensureIndex( { "key": 1 } )
db.test.ensureIndex( { "key": "hashed" } )
对于查询db.products.find( { key: "a" } ),哪一个更高性能?是hashed关键O(1)
我是怎么回答这个问题的:
在我知道你不能使用多键索引之前hashed,我创建了一个表单的索引db.test.ensureIndex( { "key": 1, "sortOrder": …
说我有一个物品文件:price和:qty字段.我有时想要找到与给定匹配的所有文件:price AND:qty,而在其他时候它将是:价格本身或:qty本身.
我已经索引了:price和:qty键,但是我还需要在两者上创建复合索引,还是单键索引足够?
编辑:我在mongodb网站上发现这篇文章非常有用:
在mongodb中创建索引时,可以指定background: true标志,这会导致索引创建为非阻塞.这在生产中非常棒,因为您不希望在创建一个之前显然不需要的索引时锁定整个数据库(因为您没有它).
阅读文档,似乎这个标志只决定了索引是如何创建的,一旦构建完毕,索引的行为就像普通索引一样.这就是我想要的 - 我不希望索引与文档不同步,因为它在后台更新,尽管我可以想象一个数据库可以做到这一点.
我在这里问,因为该getIndexes命令显示索引background在创建后仍然标记为.这只是提醒它是如何创建的吗?或者background索引在创建后表现不同?也许复制有些微妙?
我正在使用MongoDB创建我的第一个应用程序.为字段创建索引,并尝试使用$ regex param在shell中启动查找查询
> db.foo.find({A:{$regex:'BLABLA!25500[0-9]'}}).explain()
{
"cursor" : "BtreeCursor A_1 multi",
"nscanned" : 500001,
"nscannedObjects" : 10,
"n" : 10,
"millis" : 956,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
"A" : [
[
"",
{
}
],
[
/BLABLA!25500[0-9]/,
/BLABLA!25500[0-9]/
]
]
}
}
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为当我启动相同的查询,但收集中没有索引时,性能要好得多.
> db.foo.find({A:{$regex:'BLABLA!25500[0-9]'}}).explain()
{
"cursor" : "BasicCursor",
"nscanned" : 500002,
"nscannedObjects" : 500002,
"n" : 10,
"millis" : 531,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : …Run Code Online (Sandbox Code Playgroud) 我正在使用MongoDB 1.6.3来存储大集合(300k +记录).我添加了一个复合索引.
db['collection_name'].getIndexes()
[
{
"name" : "_id_",
"ns" : "db_name.event_logs",
"key" : {
"_id" : 1
}
},
{
"key" : {
"updated_at.t" : -1,
"community_id" : 1
},
"ns" : "db_name.event_logs",
"background" : true,
"name" : "updated_at.t_-1_community_id_1"
}
]
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试运行此代码时:
db['collection_name']
.find({:community_id => 1})
.sort(['updated_at.t', -1])
.skip(@skip)
.limit(@limit)
Run Code Online (Sandbox Code Playgroud)
我正进入(状态:
Mongo :: OperationFailure(没有索引的sort()数据太多.添加索引或指定更小的限制)
我究竟做错了什么?
Ι有一个数据库只有我的文件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)
但是,因为我的所有文档都是积分,所以我可以在我的架构中有一个选项,但没有太大区别. …