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. Co-Debugging JNI with Android Studio and Visual Studio

    Tutorials > Android > Integration with other tools > Co-Debugging JNI with Android Studio a ...

  2. 快学Scala-第五章 类

    知识点: 1.简单类和无参方法 class Counter { private var value = 0 //必须初始化字段 def increment() { value += 1} //方法默认 ...

  3. python插入mysql新值

    #Server Connection to MySQL: import MySQLdb conn = MySQLdb.connect(host= "localhost", user ...

  4. Ubuntu Linux系统下apt-get命令详解

    整理了Ubuntu Linux操作系统下apt-get命令的详细说明,分享给大家.常用的APT命令参数: apt-cache search package 搜索包 apt-cache show pac ...

  5. Linux下find命令用法小结

    find是个使用频率比较高的命令.常常用它在系统特定目录下,查找具有某种特征的文件. find命令的格式:find [-path……] -options [-print -exec -ok] path ...

  6. 小红的难题<递推>

    题意:五个数:N,x,y,A,B;N是台阶总数,x,y是每步可以走x或者y步,但是一定要走到A,B台阶上. 思路:学长给的题解,递推,稍微优化一点. >重点在递推 #include<cst ...

  7. ARMs3c2440开发板挂接NFS服务

    1.修改IP地址,使虚拟机,电脑PC机.开发板位于同一个网段,开发板网线与电脑网络接口连接,如PC:ip 192.168.0.112 255.255.255.0 虚拟机 192.168.0.8 255 ...

  8. USACO Section 1.2 Name That Number 解题报告

    题目 题目描述 在一个农场里面,每一头牛都有一个数字编号,但是现在这些牛不喜欢这种编号,它们想把这些数字编号转化成为可以接受的字母的形式.数字与字母的转换表如下: 2: A,B,C 5: J,K,L ...

  9. 执行 apt-get -f install 提示错误

    执行 apt-get -f install 提示错误 分类: Linux 2015-01-24 21:26 554人阅读 评论(0) 收藏 举报 1. 问题: usloft1359:~# rvm in ...

  10. Kubernetes 1.4 部署

    k8s 1.4 新版本部署 测试环境: node-: 10.6.0.140 node-: 10.6.0.187 node-: 10.6.0.188 kubernetes 集群,包含 master 节点 ...