package MongoQuickStart;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
/**
* @version 版权 Copyright(c)
* @ClassName:
* @Descripton:
* @author: Shing
* @date: 2020-12-17 18:56
*/
public class Connect {
private MongoClient mongoClient;
public Connect() {
this.mongoClient = new MongoClient("localhost",27017);
}
public MongoDatabase getDatabase(String databaseName){
return mongoClient.getDatabase(databaseName);
}
public MongoCollection<Document> getCollection(MongoDatabase mongoDatabase,String collectionName){
return mongoDatabase.getCollection(collectionName);
}
public static void main(String[] args) {
MongoClient mongoClient = new MongoClient("localhost",27017);
MongoDatabase mongoDatabase = mongoClient.getDatabase("tutorial");
MongoCollection<Document> collection = mongoDatabase.getCollection("users");
FindIterable findIterable = collection.find(Filters.eq("favories.cities","tokyo"));
MongoCursor<Document> mongoCursor = findIterable.iterator();
while (mongoCursor.hasNext()){
System.out.println(mongoCursor.next().toJson());
}
}
}
package MongoQuickStart;
import com.mongodb.Block;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.junit.Test;
import java.util.Arrays;
/**
* @version 版权 Copyright(c)
* @ClassName:
* @Descripton:
* @author: Shing
* @date: 2020-12-18 06:48
*/
public class Mongocrud {
private Connect connect = new Connect();
@Test
public void testQuery(){
MongoDatabase mongoDatabase = connect.getDatabase("tutorial");
MongoCollection<Document> collection = connect.getCollection(mongoDatabase,"users");
FindIterable<Document> findIterable = collection.find(Filters.eq("favories.cities","tokyo"));
for(Document document : findIterable){
System.out.println(document.toJson());
}
System.out.println("查询二");
collection.find(Filters.and(Filters.eq("favories.cities","tokyo"),
Filters.eq("username","adele"))).forEach(new Block<Document>() {
@Override
public void apply(Document document) {
System.out.println(document.toJson());
}
});
}
@Test
public void testInsert(){
MongoDatabase mongoDatabase = connect.getDatabase("tutorial");
MongoCollection<Document> collection = connect.getCollection(mongoDatabase,"users");
collection.insertOne(new Document("username","Shing").append("country","England")
.append("favories",new Document("cities", Arrays.asList("tokyo","shanghai"))));
}
@Test
public void testUpdate(){
MongoDatabase mongoDatabase = connect.getDatabase("tutorial");
MongoCollection<Document> collection = connect.getCollection(mongoDatabase,"users");
collection.updateOne(Filters.eq("username","adele"),
new Document("$set",new Document("favories.cities",Arrays.asList("tokyo"))));
}
}
原文:https://www.cnblogs.com/-shing/p/39f320ba776ee1ca74f5f66961784f82.html