public static void main(String[] args) {
MongoDatabase database = MongoDBAuthPoolUtil.getDatabase("数据库名");
database.createCollection("集合名字");
MongoCollection collection = database.getCollection("集合名字");
MongoDBAuthPoolUtil.dropCollection(collection);
Document document = new Document();
document.append("key", "value")
.append("username", "张三")
.append("age", 22)
.append("userlike", Arrays.asList(new String[]{"音乐", "篮球"}));
collection.insertOne(document);
List<Document> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Document docu = new Document();
docu.append("key", "value");
list.add(docu);
}
collection.insertMany(list);
collection.updateOne(Filters.eq("key", "value"), new Document("$set", new Document("key", "value")));
collection.updateOne(Filters.eq("key", "value"), new Document("$set", new Document("key", "value"))
.append("key", "value")
.append("key1", "value1"));
collection.updateMany(Filters.ne("key", null), new Document("$set", new Document("key", "value")));
collection.updateMany(Filters.ne("key", null), new Document("$set", new Document("key", "value"))
.append("key", "value")
.append("key1", "value1"));
collection.updateOne(Filters.eq("key", "value"), new Document("$puth", "aa"));
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> cursor = findIterable.iterator();
while (cursor.hasNext()) {
Document next = cursor.next();
System.out.println("next.toJson() = " + next.toJson());
String key1 = next.getString("key");
System.out.println("key1 = " + key1);
Object key = next.get("key");
System.out.println("key = " + key);
}
FindIterable iterable = collection.find(Filters.eq("_id", new ObjectId("12312sdfa132312ds")));
MongoCursor<Document> cursor1 = iterable.iterator();
while (cursor1.hasNext()) {
Document next = cursor1.next();
System.out.println("next.toJson() = " + next.toJson());
String key = next.getString("key");
System.out.println("key = " + key);
}
FindIterable<Document> iterable1 = collection.find(Filters.gt("userAge", "19"));
MongoCursor<Document> iterator = iterable1.iterator();
while (iterator.hasNext()) {
Document next = iterator.next();
System.out.println("next.toJson() = " + next.toJson());
}
FindIterable<Document> iterable2 = collection.find(Filters.type("userAge", "number"));
MongoCursor<Document> iterator2 = iterable1.iterator();
while (iterator2.hasNext()) {
Document next = iterator2.next();
System.out.println("next.toJson() = " + next.toJson());
}
FindIterable<Document> iterable3 = collection.find(Filters.in("username", "zhangsan","lisi"));
MongoCursor<Document> iterator3 = iterable1.iterator();
while (iterator3.hasNext()) {
Document next = iterator3.next();
System.out.println("next.toJson() = " + next.toJson());
}
FindIterable<Document> iterable4 = collection.find(Filters.nin("username", "zhangsan","lisi"));
MongoCursor<Document> iterator4 = iterable1.iterator();
while (iterator4.hasNext()) {
Document next = iterator4.next();
System.out.println("next.toJson() = " + next.toJson());
}
FindIterable<Document> iterable5 = collection.find(Filters.regex("username", Pattern.compile("^z.*2$")));
MongoCursor<Document> iterator5 = iterable1.iterator();
while (iterator5.hasNext()) {
Document next = iterator5.next();
System.out.println("next.toJson() = " + next.toJson());
}
FindIterable<Document> iterable6 = collection.find(Filters.and(Filters.eq("username","zhangsan"),Filters.ne("age",18)));
MongoCursor<Document> iterator6 = iterable1.iterator();
while (iterator6.hasNext()) {
Document next = iterator6.next();
System.out.println("next.toJson() = " + next.toJson());
}
FindIterable<Document> iterable7 = collection.find(Filters.or(Filters.eq("username","zhangsan"),Filters.eq("age",18)));
MongoCursor<Document> iterator7 = iterable1.iterator();
while (iterator7.hasNext()) {
Document next = iterator7.next();
System.out.println("next.toJson() = " + next.toJson());
}
FindIterable<Document> iterable18 = collection.find(Filters.or(Filters.and(Filters.eq("username","zhangsan"),Filters.eq("userage",18)),Filters.eq("like","sing")));
MongoCursor<Document> iterator18 = iterable1.iterator();
while (iterator18.hasNext()) {
Document next = iterator18.next();
System.out.println("next.toJson() = " + next.toJson());
}
FindIterable<Document> iterable9 = collection.find(Filters.regex("username",Pattern.compile("^Z.*"))).sort(new Document("username",-1));
MongoCursor<Document> iterator9 = iterable1.iterator();
while (iterator9.hasNext()) {
Document next = iterator9.next();
System.out.println("next.toJson() = " + next.toJson());
}
Document docu = new Document();
docu.put("username","stf");
docu.put("userage","19");
docu.put("like",Arrays.asList(new String[]{"sing","eat"}));
docu.put("date", new Date());
collection.insertOne(docu);
Document docus = new Document();
docus.put("username","stf");
docus.put("userage","19");
docus.put("like",Arrays.asList(new String[]{"sing","eat"}));
Date date = DateUtil.stringToDate("yyyy-MM-dd HH:mm:ss","2022-02-02 02:02:02");
docus.put("date",date);
collection.insertOne(docu);
FindIterable iterable8 = collection.find(Filters.eq("userBirth", date));
MongoCursor<Document> iterator1 = iterable8.iterator();
while (iterator1.hasNext()) {
Document next = iterator1.next();
String s = next.toJson();
System.out.println("s = " + s);
}
Document sum = new Document();
sum.put("$sum",1);
Document id_count = new Document();
id_count.put("_id",null);
id_count.put("count",sum);
Document group = new Document();
group.put("$group",id_count);
ArrayList<Document> lists = new ArrayList<>();
lists.add(group);
AggregateIterable aggregate = collection.aggregate(lists);
MongoCursor<Document> iterator8 = aggregate.iterator();
while (iterator8.hasNext()) {
Document next = iterator8.next();
Integer count = next.getInteger("count");
System.out.println("count = " + count);
}
Document sum1 = new Document();
sum.put("$sum","$size");
Document id_totalSize = new Document();
id_totalSize.put("_id",null);
id_totalSize.put("totalSize",sum);
Document group1 = new Document();
group1.put("$group",id_totalSize);
ArrayList<Document> lists1 = new ArrayList<>();
lists1.add(group);
AggregateIterable aggregate1 = collection.aggregate(lists1);
MongoCursor<Document> iterator10 = aggregate1.iterator();
while (iterator10.hasNext()) {
Document next = iterator10.next();
Integer totalSize = next.getInteger("totalSize");
System.out.println("totalSize = " + totalSize);
}
Document sums = new Document();
sums.put("$sum",1);
Document id_totalSizes = new Document();
id_totalSizes.put("_id",null);
id_totalSizes.put("totalSize",sums);
Document groups = new Document();
groups.put("$group",id_totalSizes);
Document gt = new Document();
gt.put("$gt",200);
Document sizes = new Document();
sizes.put("size",gt);
Document match = new Document();
match.put("$match",sizes);
ArrayList<Document> documents = new ArrayList<>();
documents.add(groups);
documents.add(match);
AggregateIterable<Document> iterator11 = collection.aggregate(documents);
MongoCursor<Document> iterator12 = iterator11.iterator();
while (iterator12.hasNext()) {
Document next = iterator12.next();
Integer totalSize = next.getInteger("totalSize");
System.out.println("totalSize = " + totalSize);
}
}
在这里插入代码片
public class MongoDBAuthPoolUtil {
private static MongoClient client = null;
static {
if (client == null) {
MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
builder.connectionsPerHost(10);
builder.connectTimeout(5000);
builder.socketTimeout(5000);
MongoCredential credential = MongoCredential.createCredential("用户名","数据库名称","用户密码".toCharArray());
ServerAddress address = new ServerAddress("localhost", 27017);
client = new MongoClient(address,credential,builder.build());
}
}
public static MongoDatabase getDatabase(String dbName) {
return client.getDatabase(dbName);
}
public static MongoCollection getCollection(String dbName, String collName){
MongoDatabase database = getDatabase(dbName);
MongoCollection collection = database.getCollection(collName);
return collection;
}
public static void dropCollection(MongoCollection collection){
collection.drop();
}
}