MongoOperations是一个很强大的接口,有了这个接口,基本上什么都搞定了。

其介绍

Interface that specifies a basic set of MongoDB operations. Implemented by {@link MongoTemplate}. Not often used but a useful option for extensibility and testability (as it can be easily mocked, stubbed, or be the target of a JDK proxy

直接上代码

实体类

package com.chzhao.mongodbtest;

import java.util.Date;

import org.springframework.data.annotation.Id;

public class Person {
public Person(String name, int age, Date birth) {
this.name = name;
this.age = age;
this.birth = birth;
} private Date birth; @Id
private String id; public Date getBirth() {
return birth;
} public void setBirth(Date birth) {
this.birth = birth;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} private int age;
}

操作类

package com.chzhao.mongodbtest;

import static org.springframework.data.mongodb.core.query.Criteria.where;

import java.util.Date;
import java.util.List; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query; import com.mongodb.Mongo; public class MongoApp {
private static final Log log = LogFactory.getLog(MongoApp.class); public static void main(String[] args) throws Exception { MongoOperations mongoOps = new MongoTemplate(new Mongo(), "zch");
mongoOps.dropCollection(Person.class);
mongoOps.remove(new Query(where("name").is("zhao")), Person.class);
DateTime zhaoBirth = new DateTime(1985, 12, 13, 18, 23, 55);
DateTime liangBirth = new DateTime(1986, 12, 13, 18, 23, 55);
mongoOps.insert(new Person("zhao", 34, zhaoBirth.toDate()));
mongoOps.insert(new Person("liang", 24, liangBirth.toDate())); List<Person> pList = mongoOps.find(new Query(where("name").is("zhao")),
Person.class);
for (Person p : pList) {
log.info(p.getName() + p.getAge());
}
DateTime someday = new DateTime(1986, 1, 13, 18, 23, 55);
List<Person> pList1 = mongoOps.find(new Query(where("birth").lt(someday)), Person.class);
for (Person p : pList1) {
log.info(p.getName() + p.getAge());
} }
}

这个Query类似lambda表达式,能做很多事情,很赞。

SpringDataMongoDB介绍(二)-MongoOperations介绍的更多相关文章

  1. {python--GIL锁}一 介绍 二 GIL介绍 三 GIL与Lock 四 GIL与多线程 五 多线程性能测试

    python--GIL锁 GIL锁 本节目录 一 介绍 二 GIL介绍 三 GIL与Lock 四 GIL与多线程 五 多线程性能测试 一 背景知识 ''' 定义: In CPython, the gl ...

  2. Lucene.Net 2.3.1开发介绍 —— 二、分词(六)

    原文:Lucene.Net 2.3.1开发介绍 -- 二.分词(六) Lucene.Net的上一个版本是2.1,而在2.3.1版本中才引入了Next(Token)方法重载,而ReusableStrin ...

  3. Lucene.Net 2.3.1开发介绍 —— 二、分词(五)

    原文:Lucene.Net 2.3.1开发介绍 -- 二.分词(五) 2.1.3 二元分词 上一节通过变换查询表达式满足了需求,但是在实际应用中,如果那样查询,会出现另外一个问题,因为,那样搜索,是只 ...

  4. Lucene.Net 2.3.1开发介绍 —— 二、分词(三)

    原文:Lucene.Net 2.3.1开发介绍 -- 二.分词(三) 1.3 分词器结构 1.3.1 分词器整体结构 从1.2节的分析,终于做到了管中窥豹,现在在Lucene.Net项目中添加一个类关 ...

  5. Lucene.Net 2.3.1开发介绍 —— 二、分词(四)

    原文:Lucene.Net 2.3.1开发介绍 -- 二.分词(四) 2.1.2 可以使用的内置分词 简单的分词方式并不能满足需求.前文说过Lucene.Net内置分词中StandardAnalyze ...

  6. Lucene.Net 2.3.1开发介绍 —— 二、分词(二)

    原文:Lucene.Net 2.3.1开发介绍 -- 二.分词(二) 1.2.分词的过程 1.2.1.分词器工作的过程 内置的分词器效果都不好,那怎么办?只能自己写了!在写之前当然是要先看看内置的分词 ...

  7. Lucene.Net 2.3.1开发介绍 —— 二、分词(一)

    原文:Lucene.Net 2.3.1开发介绍 -- 二.分词(一) Lucene.Net中,分词是核心库之一,当然,也可以将它独立出来.目前Lucene.Net的分词库很不完善,实际应用价值不高.唯 ...

  8. {Django基础十之Form和ModelForm组件}一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 ModelForm

    Django基础十之Form和ModelForm组件 本节目录 一 Form介绍 二 Form常用字段和插件 三 From所有内置字段 四 字段校验 五 Hook钩子方法 六 进阶补充 七 Model ...

  9. {Django基础九之中间件} 一 前戏 二 中间件介绍 三 自定义中间件 四 中间件的执行流程 五 中间件版登陆认证

    Django基础九之中间件 本节目录 一 前戏 二 中间件介绍 三 自定义中间件 四 中间件的执行流程 五 中间件版登陆认证 六 xxx 七 xxx 八 xxx 一 前戏 我们在前面的课程中已经学会了 ...

  10. MySQL之多表查询一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习

    MySQL之多表查询 阅读目录 一 介绍 二 多表连接查询 三 符合条件连接查询 四 子查询 五 综合练习 一 介绍 本节主题 多表连接查询 复合条件连接查询 子查询 首先说一下,我们写项目一般都会建 ...

随机推荐

  1. 【POJ】1084 Square Destroyer

    1. 题目描述由$n \times n, n \in [1, 5]$的正方形由$2 \times n \times (n+1)$根木棍组成,可能已经有些木棍被破坏,求至少还需破坏多少木根,可以使得不存 ...

  2. R programming, In ks.test(x, y) : p-value will be approximate in the presence of ties

    Warning message: In ks.test(x, y) : p-value will be approximate in the presence of ties   The warnin ...

  3. C++中求两个正整数的最大公约数和最小公倍数

    最大公约数直接用辗转相除法,最小公倍数就是两个数的乘积除以最大公约数 #include<iostream> using namespace std; int gys(int x,int y ...

  4. LA 5846 (计数) Neon Sign

    从反面考虑,统计非单色三角形的个数. 如果从一个点出发两条不同颜色的边,那么这三个点一定构成一个非单色三角形. 枚举一个顶点,统计从这个点出发的红边的个数a[i]和蓝边的个数n - 1 - a[i], ...

  5. textview设置字体的行距和字间距

    字间距 textView有一个属性android:textScaleX是调节字间距的,它的值是一个float型.查看源代码,默认textView 此属性是使用的是: android.internal. ...

  6. linux kernel 模块多文件编译

    /*************************************************************************** * linux kernel 模块多文件编译 ...

  7. busybox filesystem ts_config: No such file or directory

    /******************************************************************** * busybox filesystem ts_config ...

  8. fmri当前相关软件工具整理

    1.spm; 2.afni; 3.fsl; 4.drtools; 5.prtools; 6.phycaa+; 7.cca-fmri;

  9. LeetCode: Edit Distance && 子序列题集

    Title: Given two words word1 and word2, find the minimum number of steps required to convert word1 t ...

  10. Darwin Streaming Server用vs2005编译运行过程

    原创. 一:编译 Darwin6.0.3版本是最新版本,也提供了.dsw文件.但是使用vs2005和vc6是编译不过的.所以,采用Darwin5.5.5版本.使用vc6打开WinNTSupport文件 ...