package mongoDb;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

public class MongoDBUtil {
static MongoClient client;
static DB db;
static DBCollection collection;

static {
try {
// client = new MongoClient("192.168.20.114", 27017);
System.out.println("连接服务器测试.................");
ServerAddress serverAddress = new ServerAddress("localhost", 27017);
List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds.add(serverAddress);
MongoCredential credentials = MongoCredential.createMongoCRCredential("abc", "fmr",
"abc".toCharArray());
List<MongoCredential> credentialsList = new ArrayList<MongoCredential>();
credentialsList.add(credentials);
client = new MongoClient(seeds, credentialsList);
// (List<ServerAddress> seeds, List<MongoCredential//
db = client.getDB("fmr");
// db.authenticate("testAdmin", "123".toCharArray());
collection = db.getCollection("user");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void add(DBObject dbObject) {
collection.insert(dbObject);
}

public static void del(DBObject dbObject) {
collection.remove(dbObject);
}

public static void update(DBObject dbObject1, DBObject dbObject2) {
collection.update(dbObject1, dbObject2);
}

public static DBObject queryOneByKey(DBObject dbObject, DBObject keys) {
DBCursor dbCursor = collection.find(dbObject, keys);
if (dbCursor.hasNext()) {
return dbCursor.next();
}
return null;
}

public static java.util.List<DBObject> query(DBObject dbObject) {
DBCursor dbCursor = collection.find(dbObject);
List<DBObject> ret = new ArrayList<DBObject>();
while (dbCursor.hasNext()) {
DBObject dbObject2 = dbCursor.next();
ret.add(dbObject2);

}
return ret;
}



public static void main(String[] args) {
System.out.println(db.collectionExists("poepeo"));
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("num", new BasicDBObject("$gt", 0));
List<DBObject> ret = MongoDBUtil.query(dbObject);
for (Iterator<DBObject> it = ret.iterator(); it.hasNext();) {
System.out.println(it.next());
}
}

}

mongodb 3.x connect with credential的更多相关文章

  1. 3.从Node.js操作MongoDB文档

    1.更新文档结构,而非SQL 2.数据库更新运算符 在MongoDB中执行对象的更新时,需要确切的指定需要改变什么字段.需要如何改变.不像SQL语句建立冗长的查询字符串来定义更新. MongoDB中可 ...

  2. 1.从Node.js链接到MongoDB

    MongoDB采用了MongoDB Node.js驱动程序作为标准. 1.安装MongoDB驱动 npm install mongoDB npm install mongoose require('m ...

  3. Node.js 中MongoDB的基本接口操作

    Node.js 中MongoDB的基本接口操作 连接数据库 安装mongodb模块 导入mongodb模块 调用connect方法 文档的增删改查操作 插入文档 方法: db.collection(& ...

  4. 【Node.js】二、基于Express框架 + 连接MongoDB + 写后端接口

    在上节,我们讲了如何搭建express环境,现在我们说说如何通过node.js写服务接口给前端调用 1. 首先通过MongoDB建好数据库与表格 例如,我的数据库名字为db_demo,数据库表格为go ...

  5. 使用node操作mongodb

    let mongodb = require('mongodb'); let MongodbClient = mongodb.MongoClient; MongodbClient.connect('mo ...

  6. MongoDB日常运维操作命令小结

    总所周知,MongoDB是一个NoSQL非数据库系统,即一个数据库可以包含多个集合(Collection),每个集合对应于关系数据库中的表:而每个集合中可以存储一组由列标识的记录,列是可以自由定义的, ...

  7. Sample Credential Providers

        Windows Vista Sample Credential Providers Overview Contents Terms of Use Release Notes SampleCre ...

  8. mongodb之 mongodump 与 mongorestore

    一.备份 和之前介绍的 mongoexport 的数据导出工具不同, mongodump 是将数据以二进制形式导出,而 mongoexport 导出的数据格式为 csv 或 json 格式: mong ...

  9. Java 调用 groovy 脚本文件,groovy 访问 MongoDB

    groovy 访问 MongoDB 示例: shell.groovy package db import com.gmongo.GMongoClient import com.mongodb.Basi ...

随机推荐

  1. Servlet事件监听器

    监听器就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即被执行. 面试题:请描述一下java事件监听机 ...

  2. PAT乙1003

    这次终于觉得智商不够用了,特么的. 总结给你的经验,对于这样字符串的题目,经常会出现一种叫做递归定义的东西. 还有一种叫做,相同的字母表示相同的字符串. 这道题目一共有三个条件. 1. 字符串中必须仅 ...

  3. AdminLTE的使用

    1.AdminLTE的必要配置文件<!-- Tell the browser to be responsive to screen width --> <meta content=& ...

  4. Apache添加mime类型

    今天同事给我提了一个bug,说IE浏览器无法下载网站上的固件版本文件(xxxx.img),点击下载后显示一堆二进制乱码,因为我们公司的固件版本文件是以.img结尾的,所以对应的content-type ...

  5. cisco 密码重置

    密码重置 分类: 转贴技术资料 2007-12-28 16:38 http://www.cisco.com/en/US/products/hw/routers/ps259/products_passw ...

  6. STM32片上Flash内存映射、页面大小、寄存器映射

    STM32片上Flash内存映射.页面大小.寄存器映射 STM32有4种Flash module organization,分别是:low-density devices(32KB,1KB/page) ...

  7. jquery has deprecated synchronous XMLHTTPRequest

    Like many others, my website is using jquery. When I open the developer tools, I see a warning that ...

  8. CakePHP下使用paginator需要对多个字段排序的做法

      原文:http://blog.csdn.net/kunshan_shenbin/article/details/7644603  CakePHP下使用paginator需要对多个字段排序的做法 2 ...

  9. div+css与table布局

    1:速度和加载方式方面的区别 div 的加载方式是即读即加载,遇到 <div> 没有遇到 </div> 的时候一样加载 div 中的内容,读多少加载多少:table 的加载方式 ...

  10. 智力大冲浪(riddle)

    智力大冲浪(riddle) 题目描述 小伟报名参加中央电视台的智力大冲浪节目.本次挑战赛吸引了众多参赛者,主持人为了表彰大家的勇气,先奖励每个参赛者m元.先不要太高兴!因为这些钱还不一定都是你的?!接 ...