mongodb4.0和java 3.9 driver

网上到的代码很多都是旧的了, 啃下了有些官方文档, 新的东西还是官方文档最实际, 麻烦的是上班的环境没得上网, 基本都是用手机啃得

http://mongodb.github.io/mongo-java-driver/3.9/whats-new/

http://mongodb.github.io/mongo-java-driver/3.9/driver/

里面的日期最需要重点关注, 如果是使用Document 插入的话, 日期没有问题,

但是如果是JSON 字符串的话, 使用ISODate(), MongoDB 的date类型以UTC(Coordinated Universal Time)存储,就等于GMT(格林尼治标准时)时间, 0 时区的, 而我们本地的时区(北京时间)是+8 的, 导致显示的数据和我们插入的要差异, 要特殊处理一下, 插入的话, 需要用到

simpleDateFormat.setCalendar(new GregorianCalendar(new SimpleTimeZone(0,"GMT")));

import com.mongodb.client.MongoClients;
import com.mongodb.ConnectionString;
import com.mongodb.client.MongoClient;

import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.text.SimpleDateFormat;
import java.util.*;


public class MongoDBJDBC{
    public static void main( String args[] ){
        try{
            // 连接到 mongodb 服务
            MongoClient mongoClient = MongoClients.create("mongodb://godber:godber@localhost:27000/godb");
            // 连接到数据库
            MongoDatabase db = mongoClient.getDatabase("godb");

            MongoCollection<Document> collection = db.getCollection("ta_test");

            Document doc = new Document();
            doc.append("name", "test")
                    .append("val", 12)
                    .append("rate","192")
                    .append("dt",
                            new SimpleDateFormat("M/dd/yyy h:mm:ss").parse("1/24/2019 12:19:55 AM"));//1/24/2019 12:19:55 AM
                    //.append("dt",Calendar.getInstance().getTime());
                    //.append("versions", Arrays.asList("1","2"))
                    //.append("info", new Document("x",1));
            //collection.insertOne(doc);

            //List<Document> documentList = new ArrayList<Document>();

            //collection.insertMany(documentList);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
            simpleDateFormat.setCalendar(new GregorianCalendar(new SimpleTimeZone(0,"GMT")));
            String json = "{" +
                    " 'name' : 'testjs' " +
                    " ,'val' : " + 15 + " " +
                    " ,'rate' : '192' " +
                    " ,'dt' : ISODate('" + simpleDateFormat.format(
                                Calendar.getInstance().getTime()
                           )+ "')" +
                  //  " ,'dt' : ISODate('2011-11-11T00:00:00Z')" +
                    "}";
            Document document2 = Document.parse(json);
            collection.insertOne(document2);

            System.out.println("ta_test count" + collection.countDocuments());

            Document myDoc = collection.find().first();
            System.out.println(myDoc.toJson());

        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        }
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值