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. Swift -> Optional嵌套 探讨

    准备运动:Optional 的介绍 王巍的<Swifter>一书中,介绍了一个有用的命令:在 LLDB 中输入 fr v -R foo,可以查看foo 这个变量的内存构成.我们稍后的分析将 ...

  2. db2数据导出导入

    C:\Users\yexuxia>set db2instance=TCASHMAN C:\Users\yexuxia>db2(c) Copyright IBM Corporation 19 ...

  3. sonar tomacat配置

    最近在学习Sonar,配置了好几天,才搭建起来环境,为自己的学习能力感到汗颜,赶紧在此记录一下,所谓好记性不如烂笔头. 1.Sonar介绍 Sonar是一个用于代码质量管理的开源平台,用于管理Java ...

  4. JS定时器的使用--数码时钟

    <title>无标题文档</title> <script> function toDou(n){ if(n<10){ return '0'+n; }else{ ...

  5. pat L2-006. 树的遍历

    L2-006. 树的遍历 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历 ...

  6. 3--OC -- 点语法

    3.OC -- 点语法 1.方法名 // 冒号也是属于方法名的一部分 - (void)setAge:(int)age; // 方法名是 setAge: - (void)setAge; // 方法名是 ...

  7. 使用Log4net记录日志

    首先说说为什么要进行日志记录.在一个完整的程序系统里面,日志系统是一个非常重要的功能组成部分.它可以记录下系统所产生的所有行为,并按照某种规范表达出来.我们可以使用日志系统所记录的信息为系统进行排错, ...

  8. mysql查询数据库约束

    SELECT * FROM information_schema.`TABLE_CONSTRAINTS` where TABLE_SCHEMA='mold' and TABLE_NAME='tplmi ...

  9. 关于数据结构的10个面试题(c语言实现)

    关于数据结构的10个面试题(c语言实现) 2010-04-21 22:17 5702人阅读 评论(0) 收藏 举报 数据结构面试c语言bttree 1.         输入一个链表的头结点,从尾到头 ...

  10. 在 WinCe 平台读写 ini 文件

    在上篇文章开发 windows mobile 上的今日插件时,我发现 wince 平台上不支持例如 GetPrivateProfileString 等相关 API 函数.在网络上我并没有找到令我满意的 ...