标签:cep 迭代 rgs student col sage print 获取 size
mongoDB数据库、表、记录等创建好
要下载MongoDB的jar包,例如 mongo-java-driver-3.2.2.jar
连接服务器、连接数据库、连接表、创建迭代器、创建游标、滚动游标、读取记录、读取字段值
package com.gs;
import org.bson.Document;
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 HelloMongodb
{
public static void main(String[] args)
{
try
{
/*
* MongoClient 连接服务器
* MongoDatabase 连接数据库
* MongoCollection 连接表
* FindIterable<Document> 记录型迭代器
* MongoCursor 记录游标
* 应用顺序: 服务器-->数据库-->表-->记录迭代器-->记录游标
*/
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase mongoDatabase = mongoClient.getDatabase("mywork");
MongoCollection<Document> collection = mongoDatabase.getCollection("student");
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
/*
* 游标滚动-->获取记录-->读取字段值
*/
while(mongoCursor.hasNext()){
Document studentDocument = mongoCursor.next();
System.out.println(studentDocument.getString("name") +", " );
//System.out.println(mongoCursor.next());
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
}
}
}
标签:cep 迭代 rgs student col sage print 获取 size
原文地址:https://www.cnblogs.com/coderbase/p/11318032.html