Java数据类型和MongoDB数据类型对应关系

MongoDB基础 04/28 阅读 11875 views次 人气 0
摘要:

Java的数据类型与Mongodb的数据类型对应关系。

Java数据类型和MongoDB数据类型对应关系: Java boolean类型  ----------------------------------  Mongodb Boolean类型 Java char类型       ----------------------------------  Mongodb String类型 Java String类型    ----------------------------------  Mongodb String类型 Java byte类型       ----------------------------------  Mongodb 32-bit integer类型 Java short类型     ----------------------------------  Mongodb 32-bit integer类型 Java int类型         ----------------------------------  Mongodb 32-bit integer类型 Java int类型         ----------------------------------  Mongodb 32-bit integer类型 Java long类型       ----------------------------------  Mongodb 64-bit integer类型 Java float类型      ----------------------------------  Mongodb Double类型 Java double类型   ----------------------------------  Mongodb Double类型 Java util.Date类型 ----------------------------------  Mongodb Date类型 Java Array类型     ----------------------------------  Mongodb Array类型   案例MongoContext源码:
package com.what21.mdb.dao;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

/**
 */
public final class MongoContext implements Serializable {

	private static final long serialVersionUID = 1L;

	// 服务地址
	private String host;
	// 服务端口
	private int port;
	// 是否认证
	private boolean auth = false;
	// 使用数据库
	private String database;
	// 连接用户名
	private String username = "";
	// 连接密码
	private String password = "";
	
	private MongoClient client;
	
	public void setHost(String host) {
		this.host = host;
	}
	
	public void setPort(int port) {
		this.port = port;
	}
	
	public boolean isAuth() {
		return auth;
	}
	
	public void setAuth(boolean auth) {
		this.auth = auth;
	}
	
	public void setDatabase(String database) {
		this.database = database;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	private void init(){
		try {
			// 连接地址
			List<ServerAddress> serverList = new ArrayList<ServerAddress>();
			serverList.add(new ServerAddress(this.host, this.port));
			// 连接池参数
			MongoClientOptions.Builder builder = MongoClientOptions.builder();
			builder.connectionsPerHost(50);  
			builder.threadsAllowedToBlockForConnectionMultiplier(50); 
			builder.maxWaitTime(1000*60*2);  
			builder.connectTimeout(1000*60*1);   
			MongoClientOptions mco = builder.build(); 			
			if(this.auth){
				// 认证信息
				List<MongoCredential> authList = new ArrayList<MongoCredential>();
				char[] password = this.password.toCharArray();
				authList.add(MongoCredential.createCredential(this.username, 
						this.database, password));
				// 创建连接对象
				client = new MongoClient(serverList, authList, mco);
			}else{
				// 创建连接对象
				client = new MongoClient(serverList, mco);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @return
	 */
	public DB getDatabase(){
		if(client==null){
			init();
		}
		return client.getDB(this.database);
	}
	
	/**
	 * 关闭
	 */
	public void close(){
		if(client!=null){
			client.close();
			client=null;
		}
	}
	
}
案例DataTypeDao源码:
package com.what21.mdb.dao.type;

import java.util.Date;
import java.util.Set;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;
import com.what21.mdb.dao.MongoContext;

public class DataTypeDao {

	private MongoContext context;

	public DataTypeDao(MongoContext context){
		this.context = context;
	}
	
	/**
	 * 保存各种数据类型
	 * 
	 * @param docName
	 */
	public boolean save(String docName){
		// 数据库集合
		DBCollection dbColl = context.getDatabase().getCollection(docName);
		// 操作集合
		BasicDBObject attrs = new BasicDBObject(); 
		// 布尔类型
		attrs.put("boolean", true);
		// 字符类型
		attrs.put("char", 'A');
		// 字符串类型
		attrs.put("string", "");
		// 数值类型
		attrs.put("number", 123);
		// byte类型
		attrs.put("byte", (byte)123);
		// short类型
		attrs.put("short", (short)123456);
		// int类型
		attrs.put("minInt", Integer.MIN_VALUE);
		attrs.put("maxInt", Integer.MAX_VALUE);
		// long类型
		attrs.put("minLong", Long.MIN_VALUE);
		attrs.put("maxLong", Long.MAX_VALUE);
		// float类型
		attrs.put("minFloat", Float.MIN_VALUE);
		attrs.put("maxFloat", Float.MAX_VALUE);
		// double类型
		attrs.put("minDouble", Double.MIN_VALUE);
		attrs.put("maxDouble", Double.MAX_VALUE);
		// date类型
		attrs.put("date", new Date());
		// int数组类型
		attrs.put("intArray", new int[]{1,2,3,4,5});
		// double数组类型
		attrs.put("doubleArray", new double[]{1,2,3,4,5});
		// 字符串数组类型
		attrs.put("strArray", new String[]{"1111","2222","3333"});
		// 日期数组
		attrs.put("dateArray", new Date[]{new Date(),new Date(),new Date()});
		WriteResult result = dbColl.insert(attrs);
		return result.getN() > 0 ? true : false; 
	}
	
	public boolean findOne(String docName){
		// 数据库集合
		DBCollection dbColl = context.getDatabase().getCollection(docName);
		// 查询一条数据
		DBObject objAttrs = dbColl.findOne();
		// 所有返回内容
		Set<String> keySet = objAttrs.keySet();
		for(String key : keySet){
			Object value = objAttrs.get(key);
			String type = value.getClass().getName();
		 	System.out.println("key: " + key + ",值类型:" + type);
		}
		boolean bool = (Boolean)objAttrs.get("boolean");
		System.out.println("key: boolean ,value:" + bool);
		String chas = (String)objAttrs.get("char");
		System.out.println("key: char ,value:" + chas);
		String str = (String)objAttrs.get("string");
		System.out.println("key: string ,value:" + str);
		int maxInt = (Integer)objAttrs.get("maxInt");
		System.out.println("key: maxInt ,value:" + maxInt);
		long maxLong = (Long)objAttrs.get("maxLong");
		System.out.println("key: maxInt ,value:" + maxLong);
		double maxFloat = (Double)objAttrs.get("maxFloat");
		System.out.println("key: maxFloat ,value:" + maxFloat);
		double maxDouble = (Double)objAttrs.get("maxDouble");
		System.out.println("key: maxDouble ,value:" + maxDouble);
		Date date = (Date)objAttrs.get("date");
		System.out.println("key: date ,value:" + date);
		BasicDBList dbIntList = (BasicDBList)objAttrs.get("intArray"); 
		System.out.print("key: intArray value:");
		for(int i=0;i<dbIntList.size();i++){
			int value = (Integer)dbIntList.get(i);
			System.out.print(value + " ");
		}
		System.out.println();
		BasicDBList dbStrList = (BasicDBList)objAttrs.get("strArray"); 
		System.out.print("key: strArray value:");
		for(int i=0;i<dbStrList.size();i++){
			String value = (String)dbStrList.get(i);
			System.out.print(value + " ");
		}
		System.out.println();
		BasicDBList dbDateList = (BasicDBList)objAttrs.get("dateArray"); 
		System.out.print("key: dateArray value:");
		for(int i=0;i<dbDateList.size();i++){
			Date value = (Date)dbDateList.get(i);
			System.out.print(value + "   ");
		}
		System.out.println();
		return true;
	}
	
}
案例DataTypeTest源码:
package com.what21.mdb.dao.type;

import com.what21.mdb.dao.MongoContext;

public class DataTypeTest {
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MongoContext context = new MongoContext();
		context.setHost("127.0.0.1");
		context.setPort(27017);
		context.setDatabase("what21");
		
		DataTypeDao dtDao = new DataTypeDao(context);
		// 保存数据
		// dtDao.save("demo");
		// 查看数据类型  db.demo.findOne();
		dtDao.findOne("demo");
		context.close();
	}

}
测试数据查看: demo

评论

该文章不支持评论!

分享到: