第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

MongoDB十分鐘搞定CRUD

標(biāo)簽:
MongoDB

一、环境准备
MongoDB环境安装参照 MongoDBWindows平台安装
二、创建项目,添加MongoDB驱动依赖Jar

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.4.0</version>
</dependency>

三、创建连接

// To directly connect to a single MongoDB server
// (this will not auto-discover the primary even if it's a member of a replica set)
MongoClient mongoClient = new MongoClient();

// or
MongoClient mongoClient = new MongoClient( "localhost" );

// or
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

// or, to connect to a replica set, with auto-discovery of the primary, supply a seed list of members
MongoClient mongoClient = new MongoClient(
  Arrays.asList(new ServerAddress("localhost", 27017),
                new ServerAddress("localhost", 27018),
                new ServerAddress("localhost", 27019)));

// or use a connection string
MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017,localhost:27018,localhost:27019");
MongoClient mongoClient = new MongoClient(connectionString);

MongoDatabase database = mongoClient.getDatabase("mydb");

这是驱动api提供的几种连接数据库的方式,选一个自己喜欢的即可
四、开始CRUD

  • insert
    这里先插入一点点数据方便后面测试
        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
        MongoDatabase mongoDatabase = mongoClient.getDatabase("dbtest"); 
        MongoCollection<Document> collection = mongoDatabase.getCollection("users");
        for (int i = 0; i < 100000; i++) {
                Document document=  new Document();
                document.append("name", "white"+i);
                document.append("age",i);
                document.append("sex",i%2);
                document.append("money",i*10);
                // insert into users(....)
                collection.insertOne(document);
        } 
        mongoClient.close();
  • query
    ①条件查询
    @Test
    public void findByCondition(){
        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
        MongoDatabase db = mongoClient.getDatabase("dbtest"); 
        MongoCollection<Document> collection = db.getCollection("users");
        // select * from user
        collection.find();
        // select top 1 * from user where name = 'white11'
        collection.find(Filters.eq("name", "white11")).first();
        // select * from user where age < 10
        collection.find(Filters.lt("age", 10));
        // select * from user where age>= 70 and age<=100
        collection.find(Filters.and(Filters.lte("age", 100),Filters.gte("age", 70)));
        mongoClient.close();
    }
②排序
    @Test
    public void findBySort(){
        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
        MongoDatabase db = mongoClient.getDatabase("dbtest"); 
        MongoCollection<Document> collection = db.getCollection("users");
        // select * from user where age>= 70 and age<=100 order by sex desc ,age desc
        FindIterable<Document> sort = collection.find(Filters.and(Filters.lte("age", 100),Filters.gte("age", 70))).sort(Indexes.descending("sex","age")).projection(new Document().append("_id", 0));
        for (Document document : sort) {
            System.out.println(document);
        }
        mongoClient.close();
    }
  • update
    @Test
    public void update(){
        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
        MongoDatabase db = mongoClient.getDatabase("dbtest"); 
        MongoCollection<Document> collection = db.getCollection("users");
        // udpate users set name = '大神' where name = 'white11'
        UpdateResult updateOne = collection.updateOne(Filters.eq("name", "white11"), new Document().append("$set",new Document("name", "大神")));

        UpdateResult updateMany = collection.updateMany(Filters.lt("age", 10), new Document().append("$inc", new Document().append("age", -100)));
        // 影响行数 
        System.out.println(updateMany.getModifiedCount());

        mongoClient.close();
    }
  • delete
    @Test
    public void delete(){
        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
        MongoDatabase db = mongoClient.getDatabase("dbtest"); 
        MongoCollection<Document> collection = db.getCollection("users");
        // delete users where name = 'white10'
        collection.deleteOne(Filters.eq("name", "white10"));
        // delete users where age < 10
        collection.deleteMany(Filters.lt("age", 10));
        mongoClient.close();
    }
點(diǎn)擊查看更多內(nèi)容
6人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
JAVA開發(fā)工程師
手記
粉絲
8547
獲贊與收藏
6550

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消