mongodb client java

package com.wdh.mongo;

 

import java.util.ArrayList;

import java.util.List;

 

import org.junit.Test;

 

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;

 

/**

 * mongodb client for java

 * @version 2.13

 * */

public class MongoTest {

 

private String host= "192.168.78.132";

private int port = 27017;

private String userName = "wdh";

private char[] password = "wdh".toCharArray();

 

public void connTest()throws Exception{

ServerAddress address = new ServerAddress(host,port);

List<ServerAddress> addresses = new ArrayList<ServerAddress>();

addresses.add(address);

 

MongoCredential credential = MongoCredential.createCredential(userName, "wdh", password);

List<MongoCredential> credentials = new ArrayList<MongoCredential>();

credentials.add(credential);

 

MongoClient client = new MongoClient(addresses,credentials);

DB db = client.getDB("wdh");

DBCollection collection = db.getCollection("person");

}

 

public void findTest()throws Exception{

ServerAddress address = new ServerAddress(host,port);

List<ServerAddress> addresses = new ArrayList<ServerAddress>();

addresses.add(address);

 

MongoCredential credential = MongoCredential.createCredential(userName, "wdh", password);

List<MongoCredential> credentials = new ArrayList<MongoCredential>();

credentials.add(credential);

 

MongoClient client = new MongoClient(addresses,credentials);

DB db = client.getDB("wdh");

DBCollection collection = db.getCollection("person");

 

DBObject query = new BasicDBObject();

query.put("name", "zhangsan");

DBCursor cursor = collection.find(query);

//collection.find(query).sort(DBObject).limit(100).skip(100);

DBObject item = null;

while(cursor.hasNext()){

item =cursor.next();

System.out.println(item.get("age"));

}

}

 

public void insertTest()throws Exception{

ServerAddress address = new ServerAddress(host,port);

List<ServerAddress> addresses = new ArrayList<ServerAddress>();

addresses.add(address);

 

MongoCredential credential = MongoCredential.createCredential(userName, "wdh", password);

List<MongoCredential> credentials = new ArrayList<MongoCredential>();

credentials.add(credential);

 

MongoClient client = new MongoClient(addresses,credentials);

DB db = client.getDB("wdh");

DBCollection collection = db.getCollection("person");

 

BasicDBObject  query = new BasicDBObject();

query.append("name", "wangdonghua");

query.append("age", 31);

query.append("salary", 8888);

query.append("dept", "dev");

collection.insert(query);

 

}

 

public void removeTest()throws Exception{

ServerAddress address = new ServerAddress(host,port);

List<ServerAddress> addresses = new ArrayList<ServerAddress>();

addresses.add(address);

 

MongoCredential credential = MongoCredential.createCredential(userName, "wdh", password);

List<MongoCredential> credentials = new ArrayList<MongoCredential>();

credentials.add(credential);

 

MongoClient client = new MongoClient(addresses,credentials);

DB db = client.getDB("wdh");

DBCollection collection = db.getCollection("person");

 

DBObject query = new BasicDBObject();

query.put("name", "wangdonghua");

collection.remove(query);

}

 

public void updateTest()throws Exception{

ServerAddress address = new ServerAddress(host,port);

List<ServerAddress> addresses = new ArrayList<ServerAddress>();

addresses.add(address);

 

MongoCredential credential = MongoCredential.createCredential(userName, "wdh", password);

List<MongoCredential> credentials = new ArrayList<MongoCredential>();

credentials.add(credential);

 

MongoClient client = new MongoClient(addresses,credentials);

DB db = client.getDB("wdh");

DBCollection collection = db.getCollection("person");

 

BasicDBObject query = new BasicDBObject();

query.put("name", "wangdonghua");

DBCursor cursor = collection.find(query);

BasicDBObject item = null;

while(cursor.hasNext()){

item = (BasicDBObject)cursor.next();

item.append("age", 18);

collection.save(item);

}

}

}

 ----------------------------------相关参考-----------------------------------------------

http://docs.mongodb.org/ecosystem/tutorial/use-aggregation-framework-with-java-driver/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 MongoDB 的 `elemMatch` 运算符来在 Java 中查询包含特定元素的数组。`elemMatch` 运算符用于匹配数组中的一个或多个元素。 下面是一个示例代码,展示了如何使用 `elemMatch` 运算符进行查询: ```java import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; import org.bson.Document; public class Main { public static void main(String[] args) { // 连接到 MongoDB 服务器 MongoClient mongoClient = new MongoClient("localhost", 27017); // 选择数据库和集合 MongoDatabase database = mongoClient.getDatabase("mydb"); MongoCollection<Document> collection = database.getCollection("mycollection"); // 创建查询条件 Document query = new Document("arrayField", new Document("$elemMatch", new Document("key", "value"))); // 执行查询 MongoCursor<Document> cursor = collection.find(query).iterator(); // 遍历结果 while (cursor.hasNext()) { Document document = cursor.next(); System.out.println(document); } // 关闭连接 mongoClient.close(); } } ``` 在上述代码中,我们首先创建了一个 `MongoClient` 对象来连接到 MongoDB 服务器。然后选择了数据库和集合。接下来,我们创建了一个查询条件 `query`,使用了 `elemMatch` 运算符来匹配 `arrayField` 数组中包含 `{ "key": "value" }` 的元素。最后,我们执行查询并遍历结果。 请确保将 `localhost` 和 `27017` 替换为你的 MongoDB 服务器的主机名和端口号,`mydb` 替换为你要查询的数据库名称,`mycollection` 替换为你要查询的集合名称。另外,根据你的实际需求修改查询条件。 希望这可以帮助到你!如果还有其他问题,请随时问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值