mongo 隨記

$inc 更新修改字段值:a.沒有此字段則新增一個並把這次的值賦給它;b.有則在原來值的基礎上累加(如原始值是4這次$Inc值是2則更新後的值爲6)java

$set方法從新set了的字段的值,直接覆蓋原來的值內容。app

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);
	}

 輸出結果:spa

更新前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)code

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寫法:blog

 

//獲取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());
            }
        }

 打印輸出結果:get

----------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}
相關文章
相關標籤/搜索