mongodb的Multikey Indexes
来源:互联网 发布:网络维护考题 编辑:程序博客网 时间:2024/04/28 09:51
这是背景:
最近写了个小站,可以对图片进行tag,然后点击tag可以把有相同tag的图片列出来。运气好的是,我找到了Multikey Indexes。于是乎一切都简单了。
动手:
图片信息放入images这个collection,下面是其中一个image:
"_id": ObjectId("506d35b537e904092948363a"),
"_class": "com.wwt.cn.beauty.model.Image",
"name": "Armani_56.jpg",
"tags": {
"0": "wq",
"1": "d",
"2": "你在做什么"
},
"star": 0,
"userName": "123",
"date": ISODate("2012-10-04T07: 07: 33.479Z"),
"url": "http: \/\/www.test.com\/pic_storage\/123\/Armani_56.jpg"
}
mongoTemplate.ensureIndex(new Index().named("tag_index").on("tags", Order.ASCENDING), "image");
2)查找
List<Image> images= mongoTemplate.find(new Query(Criteria.where("tags").is(tagName)), Image.class);
问题解决。
解惑:看下mongodb的官方说明一下就清楚了,请猛击:http://docs.mongodb.org/manual/core/indexes/#index-type-multikey
贴部分说明 :
If you index a field that contains an array, MongoDB indexes each value in the array separately, in a “multikey index.”
Example
Given the following document:
{ "_id" : ObjectId("..."), "name" : "Warm Weather", "author" : "Steve", "tags" : [ "weather", "hot", "record", "april" ] }
Then an index on the tags field would be a multikey index and would include these separate entries:
{ tags: "weather" }{ tags: "hot" }{ tags: "record" }{ tags: "april" }
Queries could use the multikey index to return queries for any of the above values.
当然对于查找来说可以更简单。
更人性化些:
List<Image> images= imageRepository.findByTags(tagName);
更方便些:
Page<Image> images=imageRepository.findByTags(tagName, new PageRequest(0, 10));
原因是:
public interface ImageRepository extends PagingAndSortingRepository<Image, ObjectId> {
public List<Image> findByTags(String tags);
public Page<Image> findByTags(String tags,Pageable pageable);
}
- mongodb的Multikey Indexes
- MongoDB的Basic Indexes 和 Compound Indexes结合使用
- 关于jqGrid的multiselect,multiboxonly,multikey
- Mongodb Covered Indexes
- mongodb-manual-3.0.1 Indexes
- MongoDB 部分索引(Partial Indexes)
- 关于jqGrid的multiselect,multiboxonly,multikey shift 多选
- MongoDB 稀疏(间隙)索引(Sparse Indexes)
- indexes
- indexes
- MongoDB:The Definitive Guide 2nd笔记之特殊Indexes
- MyEclipse 总是不停的在 Update indexes
- Myeclipse中updating indexes 的作用
- update global indexes的online的程度研究(转)
- crawl结果文件夹中的index,indexes文件夹的区别
- apache 服务器的 Option Indexes,AllowOverride,Order Allow,Deny 详解
- 禁用 eclipse 启动时更新 updating indexes的方法
- 普元EOS7.5平台关于updating indexes的问题
- 虚函数原理及应用
- Get rid of all @Override errors in Eclipse
- 小米手机2连接在UBUNTU12.04下调试
- jquery博客分享一些php常用代码(五)
- Javascript 二分法解方程 求量杯的高度
- mongodb的Multikey Indexes
- 数字图像去噪典型算法及matlab实现
- html textarea标签必须成对出现
- css 命名规范及浏览器bug处理
- 存储过程:管理员表TAdmin的查询,插入,修改,删除操作
- javascript(1) 全局变量
- [Linux内核设计与实现]Linux进程管理
- Spring动态代理(一)
- Java访问权限修饰符