单条件查询 db.news.find({title:"sdfsdf"});
DBObject obj = new BasicDBObject();
obj.put( "title","sdfsdf" );
Query query=new BasicQuery(obj);
News news=mongoTemplate.findOne(query, News.class);
查询所有
List<News> list=mongoTemplate.findAll(News.class);
for(News n:list){
System.out.println(n.getTitle());
}
or 条件查找
db.news.find({"$or" : [ { "title" : "你家的宝宝是什么气质?"} , { "version" : "0"}]});
BasicDBList basicDBList=new BasicDBList();
basicDBList.add(new BasicDBObject("title","你家的宝宝是什么气质?"));
basicDBList.add(new BasicDBObject("content","<p>sdfsdf</p>"));
DBObject obj =new BasicDBObject();
obj.put("$or", basicDBList);
Query query=new BasicQuery(obj);
News news=mongoTemplate.findOne(query, News.class);
System.out.println(news.getContent());
db.user.find({username:"zfx1", $or:[ { version : 1 } , { b : 0 } ] } )
DBObject obj = new BasicDBObject();
obj.put( "username","zfx1" );
BasicDBList basicDBList=new BasicDBList();
basicDBList.add(new BasicDBObject("version",1));
basicDBList.add(new BasicDBObject("version",0));
obj.put("$or",basicDBList);
Query query=new BasicQuery(obj);
User user=mongoTemplate.findOne(query, User.class); // 查找
long a=mongoTemplate.count(query, User.class); // 得到数量
分组
@Test
public void group(){
String reduce = "function(doc, aggr){" +
" aggr.count += 1;" +
" }";
Query query = Query.query(Criteria.where("age").exists(true));
DBObject result = mongoTemplate.getCollection("test").group(new BasicDBObject("age", 1),
query.getQueryObject(),
new BasicDBObject("count", 0),
reduce);
Map<String,String> map = result.toMap();
System.out.println(map);
for (Map.Entry o : map.entrySet()) {
System.out.println(o.getKey() + " " + o.getValue());
}
}