写出Java操作Mongodb所有语法例子
时间: 2023-06-10 19:05:23 浏览: 107
mongodb java 调用例子
3星 · 编辑精心推荐
好的,以下是Java操作MongoDB的一些语法例子:
1.接MongoDB数据库:
MongoClient mongoClient = new MongoClient("localhost", 27017);
2. 选择数据库:
MongoDatabase database = mongoClient.getDatabase("mydatabase");
3. 获取集合对象:
MongoCollection<Document> collection = database.getCollection("mycollection");
4. 插入文档:
Document document = new Document("name", "John Doe").append("age", 30);
collection.insertOne(document);
5. 查询文档:
Document query = new Document("name", "John Doe");
FindIterable<Document> result = collection.find(query);
for (Document doc : result) {
System.out.println(doc.toJson());
}
6. 更新文档:
Document filter = new Document("name", "John Doe");
Document update = new Document("$set", new Document("age", 35));
collection.updateOne(filter, update);
7. 删除文档:
Document filter = new Document("name", "John Doe");
collection.deleteOne(filter);
以上是Java操作MongoDB的一些语法例子,希望可以帮助你!
阅读全文