创建索引时的格式:
  1. db.collection.ensureIndex({param},{param})
其中,第一个是索引的值,之前一直只用到了第一个,第二个参数便是索引的属性
比较重要的属性有:
  1. 名字
  2.   db.collection.ensureIndex({},{name:''})
  3.   在创建索引时,mongodb会自己给索引创建默认的名字,这种名字并不好记,我们看一下mongodb是怎么给自己命名的
  4. 唯一性
  5. 稀疏性
  6. 是否定时删除:比如过期索引
  1. > db.suoyin.getIndexes()
  2. [
  3. {
  4. "v" : ,
  5. "key" : {
  6. "_id" :
  7. },
  8. "name" : "_id_",
  9. "ns" : "test.suoyin"
  10. },
  11. {
  12. "v" : ,
  13. "key" : {
  14. "x" :
  15. },
  16. "name" : "x_1",
  17. "ns" : "test.suoyin"
  18. }
  19. ]
我们再增加一条索引y,排序方式为-1
  1. > db.suoyin.getIndexes()
  2. [
  3. {
  4. "v" : ,
  5. "key" : {
  6. "_id" :
  7. },
  8. "name" : "_id_",
  9. "ns" : "test.suoyin"
  10. },
  11. {
  12. "v" : ,
  13. "key" : {
  14. "x" :
  15. },
  16. "name" : "x_1",
  17. "ns" : "test.suoyin"
  18. },
  19. {
  20. "v" : ,
  21. "key" : {
  22. "y" : -
  23. },
  24. "name" : "y_-1",
  25. "ns" : "test.suoyin"
  26. }
  27. ]
可以看到name的命名方式是key值+下划线+value值
我们再创建一个复合索引
  1. > db.suoyin.ensureIndex({x:,y:-})
再看索引
  1. > db.suoyin.getIndexes()
  2. [
  3. {
  4. "v" : ,
  5. "key" : {
  6. "_id" :
  7. },
  8. "name" : "_id_",
  9. "ns" : "test.suoyin"
  10. },
  11. {
  12. "v" : ,
  13. "key" : {
  14. "x" :
  15. },
  16. "name" : "x_1",
  17. "ns" : "test.suoyin"
  18. },
  19. {
  20. "v" : ,
  21. "key" : {
  22. "y" : -
  23. },
  24. "name" : "y_-1",
  25. "ns" : "test.suoyin"
  26. },
  27. {
  28. "v" : ,
  29. "key" : {
  30. "x" : ,
  31. "y" : -
  32. },
  33. "name" : "x_1_y_-1",
  34. "ns" : "test.suoyin"
  35. }
  36. ]
可以看到,复合索引是多个key之前用下划线相隔,这种名字显得并不是很直观,如果再设置索引({x:1,y:1,z:1,m:1}),这样索引名字会特别长,不好记,另外一方面,mongodb对索引对名字有长度限制,现在对长度限制是125字节,为了避免长度超限,也避免获取索引时不能够很好的理解,所以我们可以自定义索引的名字
  1. > db.suoyin.ensureIndex({x:,y:,z:,m:},{name:'normal_index'})
  2. {
  3. "createdCollectionAutomatically" : false,
  4. "numIndexesBefore" : ,
  5. "numIndexesAfter" : ,
  6. "ok" :
  7. }
再次查看索引
  1. > db.suoyin.getIndexes()
  2. [
  3. {
  4. "v" : ,
  5. "key" : {
  6. "_id" :
  7. },
  8. "name" : "_id_",
  9. "ns" : "test.suoyin"
  10. },
  11. {
  12. "v" : ,
  13. "key" : {
  14. "x" :
  15. },
  16. "name" : "x_1",
  17. "ns" : "test.suoyin"
  18. },
  19. {
  20. "v" : ,
  21. "key" : {
  22. "y" : -
  23. },
  24. "name" : "y_-1",
  25. "ns" : "test.suoyin"
  26. },
  27. {
  28. "v" : ,
  29. "key" : {
  30. "x" : ,
  31. "y" : -
  32. },
  33. "name" : "x_1_y_-1",
  34. "ns" : "test.suoyin"
  35. },
  36. {
  37. "v" : ,
  38. "key" : {
  39. "x" : ,
  40. "y" : ,
  41. "z" : ,
  42. "m" :
  43. },
  44. "name" : "normal_index",
  45. "ns" : "test.suoyin"
  46. }
  47. ]
可以看到最后一个名字已经指定为normal_index,这不仅让人容易理解,而且在删除索引时,也可以使用名字作为参数
  1. > db.suoyin.dropIndex('normal_index')
  2. { "nIndexesWas" : , "ok" : }
相比较指定x_1_y_-1_z_1,使用名字来代替是比较方便的一种方法
  1. > db.suoyin.getIndexes()
  2. [
  3. {
  4. "v" : ,
  5. "key" : {
  6. "_id" :
  7. },
  8. "name" : "_id_",
  9. "ns" : "test.suoyin"
  10. }
  11. ]

索引属性 name指定的更多相关文章

  1. 索引属性 unique指定

    比较重要的属性有: 名字 db.collection.ensureIndex({},{name:''}) 在创建索引时,mongodb会自己给索引创建默认的名字,这种名字并不好记,我们看一下mongo ...

  2. 攻城狮在路上(壹) Hibernate(三)--- 属性访问、命名策略、派生属性、指定包名等

    一.hibernate访问持久化类属性的策略: 在<property>元素中的access属性用于指定Hibernate访问持久化类属性的方式. 常见的方式如下: 1.property:默 ...

  3. box-sizing属性(指定针对元素的宽度与高度的计算方法)

    在css中,使用width属性与height属性来指定元素的宽度与高度.使用box-sizing属性,可以指定用width属性与height属性分别指定的宽度值与高度值是否包含元素的内部补白区域与边框 ...

  4. 利用pandas选取某个属性符合指定条件的所有行

    最近遇到利用pandas选取指定行的需求,经常忘记,在此做下记录 选取某个属性等于特定值的所有行记录 df[(df[‘column_name’] == target_value)] 注:等于用 '== ...

  5. arcpy显示指定表的索引属性

    import arcpy feature_class = "c:/data/well.shp" # Create a list of indexes using the ListI ...

  6. Spring对外部属性文件指定的某个属性进行加密、解密

    [From] http://blog.csdn.net/ethanq/article/details/7333897 在我们开发当中,经常会用到spring框架来读取属性文件的属性值,然后使用占位符引 ...

  7. html -- contenteditable 属性:指定元素内容是否可编辑

    所有主流浏览器都支持 contenteditable 属性 定义和用法 contenteditable 属性指定元素内容是否可编辑. 注意: 当元素中没有设置 contenteditable 属性时, ...

  8. Java属性中指定Json的属性名称

    只需要使用注解"@JsonProperty(value = "pwd")" import com.fasterxml.jackson.annotation.Js ...

  9. 正则去除html标签属性保留指定标签

    /// <summary> /// 去除标签里面的属性保留IMG标签属性 /// </summary> /// <param name="strText&quo ...

随机推荐

  1. linux网络配置详解

    一:相关网络配置的文件 1.网卡名配置相关文件 网卡名命名规则文件: /etc/udev/rules.d/70-persistent-net.rules # PCI device 0x8086:0x1 ...

  2. 黑匣子_NOI导刊2010提高(06) Splay Tree

    题目描述 Black Box是一种原始的数据库.它可以储存一个整数数组,还有一个特别的变量i.最开始的时候Black Box是空的.而i等于0.这个Black Box要处理一串命令. 命令只有两种: ...

  3. 怎么在Vue中使用Base64格式的背景

    问题发生于一次mock数据,生成了base64格式的东西,因为编码包在一个变量中,不知道怎么直接在 :style 中引入. 解答1:格式background-image: url(此处是我们mock生 ...

  4. 牛客练习赛41D(思维转化)

    AC通道 要点 思路:题解中将所求进行转化\[max\{相似度\} = max\{M-不相似度\} = M-min\{不相似度\}\]因此转化为求某01串T与所给众S串的最小不相似度,而最终答案是选取 ...

  5. Helvetic Coding Contest 2016 online mirror B1

    Description The zombies are gathering in their secret lair! Heidi will strike hard to destroy them o ...

  6. DateAdapterForDay

    public class DateAdapterForDay extends XmlAdapter<String, Date> { private SimpleDateFormat dat ...

  7. 15-----jQuery补充

    jquery除了咱们上面讲解的常用知识点之外,还有jquery 插件.jqueryUI知识点 jqueryUI 官网: https://jqueryui.com/ jqueryUI 中文网: http ...

  8. CentOS 7: ping Name or service not known

    解决方法 cd到/etc/sysconfig/network-scripts/ 修改ifcfg-*(*代表的内容可通过ifconfig查看)中的ONBOOT=yes reboot ifcfg-ens3 ...

  9. 机器学习读书会的分享 - Reinforcement Learning: An Introduction 第4-6章

    我在机器学习读书会的分享slides,关于DP.MC.TD方法: https://mp.weixin.qq.com/s/r8wZw4iZwFCz0nnakutY3Q 内容如下:

  10. 如何远程连接非默认端口SQL Server

    SQL Server Management Studio建立远程SQL连接  连接的时候写: 127.0.0.1,49685\sqlexpress 记得使用逗号,不是冒号