mongo 随记

$inc 更新修改字段值:a.没有此字段则新增一个并把此次的值赋给它;b.有则在原来值的基础上累加(如原始值是4此次$Inc值是2则更新后的值为6)

$set方法重新set了的字段的值,直接覆盖原来的值内容。

public static void main(String[] args) {
		MongoCollection<Document> collect = getTable();
		Document s = new Document();
		s.put("userid", "27");
		//查看更新前userid=27的对象信息
		Document o =collect.find(s).first();
		System.out.println("更新前object:"+o);
		Document q = new Document();
		Document set = new Document();
		Document up = new Document();
		//$Inc方法更新累加字段值score原始值4此次累加值2更新后值应为6
		q.put("$inc",up.append("score", 2));
		//$set方法重新set了的字段的值
		q.put("$set",set.append("status", "已支付").append("act_time", DateUtil.DateAdd(new Date(), -1)).append("title", "购买汽车"));
		//更新userid=27的对象
		collect.updateOne(Filters.eq("userid", "27"), q);
		o =collect.find(s).first();
		System.out.println("更新后object:"+o);
	}

 输出结果:

更新前object:Document{{_id=5cf657c3e7625a20d4914b51, address=南京, title=订单, status=贷款中---, userid=27, score=4, act_time=Fri Jun 14 13:32:48 CST 2019}}
更新后object:Document{{_id=5cf657c3e7625a20d4914b51, address=南京, title=购买汽车, status=已支付, userid=27, score=6, act_time=Sun Jun 16 09:52:12 CST 2019}}

查询指定时间的数据 ,返回数据只指定某个字段返回(例如如下只返回mobile字段则设置mobile:1就好,而_id字段是默认返回,若不返回则设置_id:0)

db.getCollection('test').find(
  {
    "offerPrice" : { "$gte" : 1 , "$lt" : 12} ,
    "week_offerPrice" : { "$gte" : 1 , "$lt" : 23} ,
    "offerPrice_time" : { "$gte" : ISODate("2019-06-05T16:00:00.000Z") , "$lt" : ISODate("2019-06-11T16:00:00.000Z")}
  },
  {
    mobile:1,_id:0
  }
)

Java写法:

 

//获取dbcollection对象
        DBCollection dbx = MongoAccessObj.getDBCollection().getCollection("silei");
        BasicDBObject queryObject = new BasicDBObject();
        //指定筛选条件
        queryObject.append("act_time", new BasicDBObject(QueryOperators.LT, DateUtil.DateAdd(new Date(), -5))
                .append(QueryOperators.GTE, DateUtil.DateAdd(new Date(), -8)));
        queryObject.append("score", new BasicDBObject().append(QueryOperators.GTE, 3).append(QueryOperators.LT, 5));
        //选择默认返回的字段值
        BasicDBObject queryObject2 = new BasicDBObject();
        queryObject2.put("userid", 1);
        queryObject2.put("score", 1);
        queryObject2.put("address", 1);
        //查询开始
        DBCursor find = dbx.find(queryObject,queryObject2);
        if(find!=null) {//返回遍历
            while(find.hasNext()) {
                DBObject  obj = find.next();//_id是默认返回的
                System.out.println("----------1-----------:"+obj.toString());
            }
        }

 打印输出结果:

----------1-----------:{ "_id" : { "$oid" : "5cf657c3e7625a20d4914b51"} , "address" : "南京" , "userid" : "27" , "score" : 4}
----------1-----------:{ "_id" : { "$oid" : "5cf71ad0e7625a1288d7c2af"} , "address" : "北京" , "userid" : "30" , "score" : 4}
----------1-----------:{ "_id" : { "$oid" : "5cf71ad0e7625a1288d7c2b0"} , "address" : "北京" , "userid" : "31" , "score" : 4}
----------1-----------:{ "_id" : { "$oid" : "5cf71ad0e7625a1288d7c2b1"} , "address" : "北京" , "userid" : "32" , "score" : 4}
----------1-----------:{ "_id" : { "$oid" : "5cf71ad0e7625a1288d7c2b2"} , "address" : "北京" , "userid" : "33" , "score" : 4}
----------1-----------:{ "_id" : { "$oid" : "5cf71ad0e7625a1288d7c2b3"} , "address" : "北京" , "userid" : "34" , "score" : 4}
----------1-----------:{ "_id" : { "$oid" : "5cf71ad0e7625a1288d7c2b4"} , "address" : "北京" , "userid" : "35" , "score" : 4}
----------1-----------:{ "_id" : { "$oid" : "5cf71ad0e7625a1288d7c2b5"} , "address" : "北京" , "userid" : "36" , "score" : 4}
----------1-----------:{ "_id" : { "$oid" : "5cf71ad0e7625a1288d7c2b6"} , "address" : "北京" , "userid" : "37" , "score" : 4}
----------1-----------:{ "_id" : { "$oid" : "5cf71ad0e7625a1288d7c2b7"} , "address" : "北京" , "userid" : "38" , "score" : 4}
----------1-----------:{ "_id" : { "$oid" : "5cf71ad0e7625a1288d7c2b8"} , "address" : "北京" , "userid" : "39" , "score" : 4}

 

posted @ 2019-05-31 16:38  代码伊甸园  阅读(184)  评论(0编辑  收藏  举报