• Java 操作 MongoDB


    操作 MongoDB CRUD

    import org.bson.Document;
    
    import com.mongodb.BasicDBObject;
    import com.mongodb.MongoClient;
    import com.mongodb.client.FindIterable;
    import com.mongodb.client.MongoCollection;
    import com.mongodb.client.MongoCursor;
    import com.mongodb.client.MongoDatabase;
    
    public class TestCRUD {
    
        public static void main(String[] args) {
            MongoClient client = new MongoClient();
            MongoDatabase database = client.getDatabase("test");
            MongoCollection<Document> collection = database.getCollection("person");
            
            Document document = new Document("x",1);
            collection.insertOne(document);
            
            // 自定义类型
            MongoCollection<BasicDBObject> collection2 = database.getCollection("person",BasicDBObject.class);
            BasicDBObject basicDBObject = new BasicDBObject("y",2);
            collection2.insertOne(basicDBObject);
            
            FindIterable<Document> find = collection.find();
            MongoCursor<Document> iterator = find.iterator();
            while (iterator.hasNext()) {
                System.out.println(iterator.next().toJson());
            }
            
            client.close();
        }
    }
  • 相关阅读:
    Debug和Release区别
    C语言程序_管理系统
    读书的学问
    御姐的含义
    进制的英文书写
    CHM文件无法打开的解决方法
    819代码
    点击链接不跳转或刷新
    MS SqlServer 随机查询并随机排序
    Html背景图
  • 原文地址:https://www.cnblogs.com/bwcode/p/8512497.html
Copyright © 2020-2023  润新知