MongoDB(二)

来源:互联网 发布:nginx 正则匹配 编辑:程序博客网 时间:2024/06/05 20:04

mongodb java

        /**         * mongodb java driver 测试         */        public class MongoDB {            public static void main(String[] args) {                //连接mongodb服务                MongoClient mongoClient = null;                try {                    mongoClient = new MongoClient("192.168.134.20", 27017);                    //获取数据库                    MongoDatabase database = mongoClient.getDatabase("foobar");                    //创建集合        //          database.createCollection("demo");                    //获取集合                    MongoCollection<Document> collection = database.getCollection("demo");                    //插入一个文档        //          collection.insertOne(new Document("language","java"));        //          System.out.println(collection.findOneAndDelete(new Document("language","java")));                    //查询文档                    List<Document> list = collection.find().into(new ArrayList<Document>());                    System.out.println(list);                    //集合列表                    MongoIterable<String> iter = database.listCollectionNames();                    for (String name : iter) {                        System.out.println("集合名称:" + name);                    }                    //清空demo集合                    collection.drop();                    //集合中插入文档                    Document doc1 = new Document("name","james").append("age", 32);                    Document doc2 = new Document("name","curry").append("age", 27);                    collection.insertMany(Arrays.asList(doc1,doc2));                    //查询                    FindIterable<Document> find = collection.find();                    print(find);                    //创建单字段索引                    collection.createIndex(new Document("name",1));                    //根据条件查询                    find = collection.find(new Document("age",32));                    print(find);                    //根据正则表示式查询                    find = collection.find(Filters.and (Filters.regex("name","m"),Filters.eq("age",32)));                    print(find);                    //根据name排序                    find = collection.find().sort(Sorts.ascending("name"));                    print(find);                    //不存在新增,存在更新                    Bson fiter = Filters.eq("name", "张三");                    Bson doc = new Document("$set",new Document("name","张三").append("age", 23));                    UpdateOptions upsert = new UpdateOptions().upsert(true);                    collection.updateOne(fiter, doc, upsert);                    Bson fiterAfter = Filters.eq("name", "张三");                    Bson docAfter = new Document("$set",new Document("name","李四").append("age", 23));                    UpdateOptions upsertAfter = new UpdateOptions().upsert(true);                    collection.updateOne(fiterAfter, docAfter, upsertAfter);                    FindIterable<Document> iterable = collection.find();                    print(iterable);                } catch (Exception e) {                    e.printStackTrace();                } finally {                    if (mongoClient != null) {                        //释放资源                        mongoClient.close();                    }                }            }            private static void print(FindIterable<Document> find) {                System.out.println("----------------------");                for (Document document : find) {                    System.out.println("文档内容:"+document.toString());                }            }        }
原创粉丝点击