MongoCollection collection = mongoDatabase.getCollection("test_logs"); //加入查询条件 BasicDBObject...query = new BasicDBObject(); //时间区间查询 记住如果想根据这种形式进行时间的区间查询 ,存储的时候 记得把字段存成字符串,就按yyyy-MM-dd HH...:mm:ss 格式来 query.put("times", new BasicDBObject("$gte", "2018-06-02 12:20:00").append("$lte",
("age", new BasicDBObject("$lte", 105))); while (cursor.hasNext()) { System.out.println(cursor.next...("_id", new ObjectId("5590e57b7d51ad2ef1b69a93"))); //条件删除 collection.remove(new BasicDBObject("age...=DBUtil.getDBCollection("testtable"); //根据id修改 collection.update(new BasicDBObject("_id", new ObjectId...("5590e57b7d51ad2ef1b69a93")), new BasicDBObject("age", 99)); //条件修改 collection.update(new BasicDBObject...("age", new BasicDBObject("$gte", 105)), new BasicDBObject("age", 11)); } } 关于聚合可以看这篇文章,文章地址
= new BasicDBObject(“_id”,”$字段名”);//也就是说groupby这个字段名 groupFields.put(“SumElectricty”,new BasicDBObject...map return map; } public List sortMapByValue(Map...oriMap,int n) { List mappingList = new ArrayList();...> map = (Entry) it.next(); BasicDBObject dbob = map.getValue()...实际上就是BasicDBObject,因此直接定义这个对象的比较器就可以了。
在MongoDB中是文档存储的,也就是模式自由的,我们所取到的文档中实际上就是MongoDB中的类,DBObject(接口)BasicDBObject(实现类),这是查询获得的结果,是一种K-V结构的。...doc = new BasicDBObject(); //文档或者可以看作是k-v记录集 doc.put("name", "MongoDB");...doc.put("type", "database"); doc.put("count", 1); BasicDBObject info = new...(); query.put("i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i...(); query.put("i", 71); BasicDBObject update = new BasicDBObject();
dbObject =new BasicDBObject(); dbObject.put(“title”,“奔放洋气有深度,简约时尚国际范“); dbObject.put...//设置更新的内容的对象… BasicDBObject object = (BasicDBObject) dbCollection.findOne(query); object.put...dbObject=new BasicDBObject(); // dbObject.put(“content”, “高大上“); DBCursor cursor...basicDBObject =new BasicDBObject(); BasicDBObject basicDBObject2 =new BasicDBObject();...basicDBObject2.put(“age”, 20); basicDBObject.put(“$lt”, basicDBObject2);
用框架的语法拼出来就可以了 当然还有的小伙伴还是用习惯了java驱动的写法,也可以,就是没上面简洁 List pipeline = new ArrayList(); BasicDBObject...group = new BasicDBObject(); group.put("$group", new BasicDBObject("_id","$author") .append("count...", new BasicDBObject("$sum",1)) .append("name", new BasicDBObject("$first","$author"))); BasicDBObject...project = new BasicDBObject(); project.put("$project", new BasicDBObject("name",1) .append("
Date lastExcTime=new Date(); CommandResult result = mongoTemplate.getDb().command( new BasicDBObject(..."distinct", "homeLog").append("key", "userId").append( "query", new BasicDBObject("createTime", new BasicDBObject
添加数据 collection.save(new BasicDBObject("author", "yinjihuan").append("title", "mongodb")); ---- 编辑数据...collection.update(new BasicDBObject("author", "yinjihuan"), new BasicDBObject("$set", new BasicDBObject...("title", "mongodb3.0"))); ---- 删除数据 collection.remove(new BasicDBObject("author", "yinjihuan")); ---...", new BasicDBObject("$gt", 10)); cursor = collection.find(query); ---- 小于(lt)查询 query = new BasicDBObject...BasicDBObject("author", "yinjihuan"); DBObject fields = new BasicDBObject("author", 1); cursor = collection.find
System.out.println("浏览量:"+document.getInteger("visits")); } client.close();//关闭连接 } } 条件查询 BasicDBObject...对象:表示一个具体的记录,BasicDBObject实现了DBObject,是key- value的数据结构,用起来和HashMap是基本一致的。...spitdb");//打开数据库 MongoCollection spit = spitdb.getCollection("spit");// 获取集合 BasicDBObject...bson = new BasicDBObject("userid", "1013");// 构建查询 条件 FindIterable documents = spit.find...bson = new BasicDBObject("visits", new BasicDBObject("$gt", 1000)); // 构建查询条件 等同于find({visits:
示例:查询testCollection集合中age小于20的文档记录 //查询年龄小于20的 BasicDBObject bson=new BasicDBObject("age", new BasicDBObject...bson1=new BasicDBObject("age", new BasicDBObject("$gte",20)); //查询年龄小于30的 BasicDBObject bson2=new BasicDBObject...("age", new BasicDBObject("$lt",30)); //构建查询条件and BasicDBObject bson=new BasicDBObject("$and", Arrays.asList...new BasicDBObject("$lte",20)); BasicDBObject bson2=new BasicDBObject("sex", "女"); //构建查询条件or BasicDBObject...bson= new BasicDBObject("name", "红孩儿"); //修改后的值 BasicDBObject bson2 = new BasicDBObject("$set",new BasicDBObject
MongoDB package mymaven; import java.net.UnknownHostException; import java.util.Set; import com.mongodb.BasicDBObject...obj = new BasicDBObject(); // 初始化一个基本DB对象,插入数据库的就是DB对象 obj.put("from", "blog.ithomer.net")...// 打印出刚才插入的数据 // 插入10条{ranking:i}的数据 for (int i = 0; i < 10; i++) { collection.insert(new BasicDBObject...(); query.put("ranking", new BasicDBObject("$gte", 5).append("$lt", 9)); cursor = collection.find...obj = (BasicDBObject) cursor.next(); long id = obj.getLong("_id"); ArrayList tagList
DBObject groupFields = new BasicDBObject("\_id", null); groupFields.put("count", new BasicDBObject("$...sum", 1)); BasicDBObject group = new BasicDBObject("$group", groupFields); List aggreList...= new ArrayList(); aggreList.add(group); AggregateIterable output = collection.aggregate
BasicDBObject doc = new BasicDBObject(); doc.put("name", "MongoDB"); doc.put("type", "database");...doc.put("count", 1); BasicDBObject info = new BasicDBObject(); info.put("x", 203); info.put("y", 102...例如,我们想要查询域名为"i",并且值为71的文档: BasicDBObject query = new BasicDBObject(); query.put("i", 71); cur = coll.find...query = new BasicDBObject(); query.put("i", new BasicDBObject("$gt", 50)); cur = coll.find(query);...= new BasicDBObject(); query.put("i", new BasicDBObject(" 12 创建索引 MongoDB支持索引,而且很容易在集合上增加索引。
setObjData(Map param, String sysGuid, String enumBpd){ DBObject dbObject = new BasicDBObject...query= new BasicDBObject(); //模糊查询的字段设置 query.put("page_html", Pattern.compile((String...query= new BasicDBObject(); //批量查询,in if (CollectionUtils.isNotEmpty(ids)){...query= new BasicDBObject(); //拼接参数 if(MapUtils.isNotEmpty(paramMap)){ for...query= new BasicDBObject(); //拼接参数 if(MapUtils.isNotEmpty(paramMap)){ for
package com.cnblogs.yjmyzz.cache.test; 2 3 import com.google.gson.Gson; 4 import com.mongodb.BasicDBObject...doc = new BasicDBObject("name", "杨俊明").append("sex", "男") 53 .append("address", 54...new BasicDBObject("postcode", "201202").append( 55...docFind = new BasicDBObject("name", "杨俊明"); 60 DBObject findResult = coll.findOne(docFind);...objUser = new BasicDBObject("key", "jimmy").append( 85 "value", toDBObject(userData
client.close(); } @Test public void test2() { //查询 FindIterable documents = comment.find(new BasicDBObject...comment.insertOne(document); } 2.4 修改 @Test public void test4() { //修改的条件 Bson filter = new BasicDBObject...("_id", "6"); //修改的数据 Bson update = new BasicDBObject("$set", new Document("userid", "8888")); comment.updateOne...(filter, update); } 2.5 删除 @Test public void test5() { //删除的条件 Bson filter = new BasicDBObject("
db.getCollection(collectionName); DBObject dbObject; try { dbObject = collection.findOne(new BasicDBObject...).getDB(); collection = db.getCollection(collectionName); dbObject = collection.findOne(new BasicDBObject...this.switchCluster().getDB(); collection = db.getCollection(collectionName); dbObject = collection.findOne(new BasicDBObject...>(); BasicDBList dbList = new BasicDBList(); dbList.addAll(columnNames); DBObject inObj = new BasicDBObject...("$in", dbList); DBCursor cursor = collection.find(new BasicDBObject("column_name", inObj)); DBObject
dbCollection, String collName, List options, boolean ordered) { DBObject command = new BasicDBObject...(); command.put("update", collName); List updateList = new ArrayList(); for (BathUpdateOptions option : options) { BasicDBObject update = new BasicDBObject(
org.springframework.stereotype.Service; 22 23 import com.mongodb.BasicDBList; 24 import com.mongodb.BasicDBObject...obj = (BasicDBObject)list.get(i); 166 equipStatistics.setIp(obj.getString("logonIp"));...obj = (BasicDBObject)list.get(i); 270 System.out.println(obj.get("count")); 271...obj = (BasicDBObject)list.get(i); equipStatistics.setIp(obj.getString("logonIp"));...obj = (BasicDBObject)list.get(i); obj.getString("key"); key为initial中的key值,通过以上代码获取key值对应的value; 这只是其中一种用法
领取专属 10元无门槛券
手把手带您无忧上云