File --> New --> Java Project
MongoJava右键 --> New --> mongo
mongo右键 --> New --> Class
package mongo;
import java.util.ArrayList;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
public class mimalianjie {
public static void main(String[] args) {
try {
ServerAddress serverAddress = new ServerAddress("localhost",27017);
ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
MongoCredential credential = MongoCredential.createScramSha1Credential("root", "admin", "strongs".toCharArray());
ArrayList<MongoCredential> credentials = new
ArrayList<MongoCredential>();
credentials.add(credential);
MongoClient mongoClient = new MongoClient(addrs,credentials);
MongoDatabase mongoDatabase = mongoClient.getDatabase("databaseName");
System.out.println("Connect to database successfully");
} catch (Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
package mongo;
import java.util.ArrayList;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
public class chuanjianjihe {
public static void main(String[] args) {
try {
ServerAddress serverAddress = new ServerAddress("localhost",27017);
ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
MongoCredential credential = MongoCredential.createScramSha1Credential("root","admin","strongs".toCharArray());
ArrayList<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
MongoClient mongoClient = new MongoClient(addrs,credentials);
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
System.out.println("Connect to database successfully");
mongoDatabase.createCollection("mycol");
System.out.println("集合mycol创建成功");
}catch (Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage());
}
}
}
package mongo;
import java.util.ArrayList;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class huoqujihe {
public static void main(String[] args) {
try {
ServerAddress serverAddress = new ServerAddress("localhost",27017);
ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
MongoCredential credential = MongoCredential.createScramSha1Credential("root","admin","strongs".toCharArray());
ArrayList<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
MongoClient mongoClient = new MongoClient(addrs,credentials);
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
System.out.println("Connect to database successfully");
MongoCollection<org.bson.Document> collection = mongoDatabase.getCollection("mycol");
System.out.println("集合mycol选择成功");
} catch (Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage());
}
}
}
package mongo;
import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class charuwendang {
public static void main (String[] args) {
try {
ServerAddress serverAddress = new ServerAddress("localhost",27017);
ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
MongoCredential credential = MongoCredential.createScramSha1Credential("root"
,"admin","strongs".toCharArray());
ArrayList<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
MongoClient mongoClient = new MongoClient(addrs,credentials);
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
System.out.println("Connect to database successfully");
MongoCollection<org.bson.Document> collection = mongoDatabase.getCollection("mycol");
System.out.println("集合mycol选择成功");
Document document = new Document("name", "zhangyudashuju").
append("description", "YXCX").
append("likes", 100).
append("location", "BJ");
List<Document> documents = new ArrayList<Document>();
documents.add(document);
collection.insertMany(documents);
System.out.println("文档插入成功");
}catch(Exception e) {
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
package mongo;
import java.util.ArrayList;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
public class jiansuosuoyouwendang {
public static void main( String args[] ){
try{
ServerAddress serverAddress = new ServerAddress("localhost",27017);
ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
MongoCredential credential = MongoCredential.createScramSha1Credential("root"
,"admin", "strongs".toCharArray());
ArrayList<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
MongoClient mongoClient = new MongoClient(addrs,credentials);
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
System.out.println("Connect to database successfully");
MongoCollection<org.bson.Document> collection = mongoDatabase.getCollection("mycol");
System.out.println("集合mycol选择成功");
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
package mongo;
import java.util.ArrayList;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
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;
public class gengxinwendang {
public static void main( String args[] ){
try{
ServerAddress serverAddress = new ServerAddress("localhost",27017);
ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
MongoCredential credential = MongoCredential.createScramSha1Credential("root"
,"admin", "strongs".toCharArray());
ArrayList<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
MongoClient mongoClient = new MongoClient(addrs,credentials);
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
System.out.println("Connect to database successfully");
MongoCollection<org.bson.Document> collection = mongoDatabase.getCollection("mycol");
System.out.println("集合mycol选择成功");
collection.updateMany(Filters.eq("likes", 100), new Document("$set",new Document("likes",200)));
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
package mongo;
import java.util.ArrayList;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
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;
public class shanchuwendang {
public static void main( String args[] ){
try{
ServerAddress serverAddress = new ServerAddress("localhost",27017);
ArrayList<ServerAddress> addrs = new ArrayList<ServerAddress>();
addrs.add(serverAddress);
MongoCredential credential = MongoCredential.createScramSha1Credential("root"
,"admin", "strongs".toCharArray());
ArrayList<MongoCredential> credentials = new ArrayList<MongoCredential>();
credentials.add(credential);
MongoClient mongoClient = new MongoClient(addrs,credentials);
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
System.out.println("Connect to database successfully");
MongoCollection<org.bson.Document> collection = mongoDatabase.getCollection("mycol");
System.out.println("集合mycol选择成功");
//删除符合条件的第一个文档
//collection.deleteOne(Filters.eq("likes", 200));
//删除所有符合条件的文档
collection.deleteMany (Filters.eq("likes", 200));
//检索查看结果
FindIterable<Document> findIterable = collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
查询结果为空,证明文档已被删除。
至此,实验结束!