Created
June 23, 2017 05:47
-
-
Save killme2008/06dfdfb83f3b71d6f88f18569f3f1a73 to your computer and use it in GitHub Desktop.
Mongodb test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.bson.Document; | |
import com.mongodb.MongoClient; | |
import com.mongodb.client.MongoCollection; | |
import com.mongodb.client.MongoDatabase; | |
public class MongodbTest { | |
public static void main(String args[]) throws Exception { | |
// 连接到 mongodb 服务 | |
MongoClient mongoClient = new MongoClient("localhost", 27017); | |
// 连接到数据库 | |
MongoDatabase mongoDatabase = mongoClient.getDatabase("test"); | |
MongoCollection<Document> collection = mongoDatabase.getCollection("Users"); | |
// 插入文档 | |
Document document = | |
new Document("title", "MongoDB").append("description", "database").append("likes", 100) | |
.append("by", "Fly"); | |
collection.insertOne(document); | |
System.out.println("文档插入成功"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment