MongoDB基础:通过JAVA的增删改查进门
首先/一开始MongoDB是NoSQL世界中的代表性文档型数据库,并且其CRUD操作的实现非常简单。
我们可以使用JAVA编写API来简便地进行CRUD操作。
引入驱动程序库
// https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver
compileInclude group: 'org.mongodb', name: 'mongo-java-driver', version: '3.11.0'
创建
插入图像
Java插入示例
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// ユーザーの属性をセット
Document user = new Document();
user.append("lastName", "tanaka");
user.append("firstName", "ichiro");
user.append("gender", "男性");
// DBにインサート
users.insertOne(user);
// クライントを閉じる
client.close();
}
}
收藏图像 tú
多個插入
List<Document> userList = new ArrayList<Document>();
Document user1 = new Document();
user1.append("lastName", "tanaka");
Document user2 = new Document();
user2.append("lastName", "yamada");
userList.add(user1);
userList.add(user2);
// リストをDBにインサート
users.insertMany(userList);
阅读 (Yuè dú)
搜索图像
找一个
可以使用类似于 `db.collection.find(filter).first()` 的方式来获取第一条数据。如果想通过 _id 进行搜索,可以使用 `first` 方法非常方便。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// _idで検索する
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
if (user != null) {
System.out.println("lastName:" + user.getString("lastName"));
}
// クライントを閉じる
client.close();
}
}
努力工作
姓:田中
查找列表
搜索示例
// 姓で複数人を検索する
MongoCursor<Document> cursor = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).cursor();
while (cursor.hasNext()) {
Document user = (Document) cursor.next();
// do something
}
// Cursorではなく、リストに変換しても便利です。
// List<Document> documents = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).into(new ArrayList<Document>());
排序、跳过、限制等
Document sort = new Document();
sort.append("updateDate", -1);
MongoCursor<Document> cursor = users.find(Filters.in("lastName", "tanaka", "yamada")).sort(sort).skip(20).limit(10).cursor();
更新
更新的图片 de
第一次更新
使用updateOne方法更新一条数据。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Date;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document updateSet = new Document();
updateSet.append("lastName", "鈴木");
updateSet.append("updateDate", new Date());
// updateオブジェクト
Document update = new Document();
update.append("$set", updateSet);
// 更新
users.updateOne(filter , update);
// クライントを閉じる
client.close();
}
}
更新后的收藏品数据
更新许多
更新文档的指南:https://docs.mongodb.com/manual/tutorial/update-documents/
简单替换一个
如果要替换文档的全部数据而不是部分更新,可以使用replaceOne方法。
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document update = new Document();
update.append("lastName", "鈴木");
update.append("updateDate", new Date());
// 差し替え
users.replaceOne(filter, update);
在处理将DB中搜索的文档作为替换数据时需要注意一点,这可能会导致错误。
错误示例:
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
// 差し替え(違う_idなので、エラーになる。同じ_idなら問題ありません。)
users.replaceOne(Filters.eq("_id", new ObjectId("5d859c77b64c3286a4d29d90")), user);
如果不同的_id,则删除用户的_id字段可能会解决错误。
如果存在版本数据,则可以考虑使用replaceOne方法。
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
user.remove("_id");
删除
删除图像
删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above
// https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver
compileInclude group: 'org.mongodb', name: 'mongo-java-driver', version: '3.11.0'
创建
插入图像
Java插入示例
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// ユーザーの属性をセット
Document user = new Document();
user.append("lastName", "tanaka");
user.append("firstName", "ichiro");
user.append("gender", "男性");
// DBにインサート
users.insertOne(user);
// クライントを閉じる
client.close();
}
}
收藏图像 tú
多個插入
List<Document> userList = new ArrayList<Document>();
Document user1 = new Document();
user1.append("lastName", "tanaka");
Document user2 = new Document();
user2.append("lastName", "yamada");
userList.add(user1);
userList.add(user2);
// リストをDBにインサート
users.insertMany(userList);
阅读 (Yuè dú)
搜索图像
找一个
可以使用类似于 `db.collection.find(filter).first()` 的方式来获取第一条数据。如果想通过 _id 进行搜索,可以使用 `first` 方法非常方便。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// _idで検索する
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
if (user != null) {
System.out.println("lastName:" + user.getString("lastName"));
}
// クライントを閉じる
client.close();
}
}
努力工作
姓:田中
查找列表
搜索示例
// 姓で複数人を検索する
MongoCursor<Document> cursor = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).cursor();
while (cursor.hasNext()) {
Document user = (Document) cursor.next();
// do something
}
// Cursorではなく、リストに変換しても便利です。
// List<Document> documents = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).into(new ArrayList<Document>());
排序、跳过、限制等
Document sort = new Document();
sort.append("updateDate", -1);
MongoCursor<Document> cursor = users.find(Filters.in("lastName", "tanaka", "yamada")).sort(sort).skip(20).limit(10).cursor();
更新
更新的图片 de
第一次更新
使用updateOne方法更新一条数据。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Date;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document updateSet = new Document();
updateSet.append("lastName", "鈴木");
updateSet.append("updateDate", new Date());
// updateオブジェクト
Document update = new Document();
update.append("$set", updateSet);
// 更新
users.updateOne(filter , update);
// クライントを閉じる
client.close();
}
}
更新后的收藏品数据
更新许多
更新文档的指南:https://docs.mongodb.com/manual/tutorial/update-documents/
简单替换一个
如果要替换文档的全部数据而不是部分更新,可以使用replaceOne方法。
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document update = new Document();
update.append("lastName", "鈴木");
update.append("updateDate", new Date());
// 差し替え
users.replaceOne(filter, update);
在处理将DB中搜索的文档作为替换数据时需要注意一点,这可能会导致错误。
错误示例:
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
// 差し替え(違う_idなので、エラーになる。同じ_idなら問題ありません。)
users.replaceOne(Filters.eq("_id", new ObjectId("5d859c77b64c3286a4d29d90")), user);
如果不同的_id,则删除用户的_id字段可能会解决错误。
如果存在版本数据,则可以考虑使用replaceOne方法。
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
user.remove("_id");
删除
删除图像
删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above

Java插入示例
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// ユーザーの属性をセット
Document user = new Document();
user.append("lastName", "tanaka");
user.append("firstName", "ichiro");
user.append("gender", "男性");
// DBにインサート
users.insertOne(user);
// クライントを閉じる
client.close();
}
}
收藏图像 tú
多個插入
List<Document> userList = new ArrayList<Document>();
Document user1 = new Document();
user1.append("lastName", "tanaka");
Document user2 = new Document();
user2.append("lastName", "yamada");
userList.add(user1);
userList.add(user2);
// リストをDBにインサート
users.insertMany(userList);
阅读 (Yuè dú)
搜索图像
找一个
可以使用类似于 `db.collection.find(filter).first()` 的方式来获取第一条数据。如果想通过 _id 进行搜索,可以使用 `first` 方法非常方便。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// _idで検索する
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
if (user != null) {
System.out.println("lastName:" + user.getString("lastName"));
}
// クライントを閉じる
client.close();
}
}
努力工作
姓:田中
查找列表
搜索示例
// 姓で複数人を検索する
MongoCursor<Document> cursor = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).cursor();
while (cursor.hasNext()) {
Document user = (Document) cursor.next();
// do something
}
// Cursorではなく、リストに変換しても便利です。
// List<Document> documents = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).into(new ArrayList<Document>());
排序、跳过、限制等
Document sort = new Document();
sort.append("updateDate", -1);
MongoCursor<Document> cursor = users.find(Filters.in("lastName", "tanaka", "yamada")).sort(sort).skip(20).limit(10).cursor();
更新
更新的图片 de
第一次更新
使用updateOne方法更新一条数据。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Date;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document updateSet = new Document();
updateSet.append("lastName", "鈴木");
updateSet.append("updateDate", new Date());
// updateオブジェクト
Document update = new Document();
update.append("$set", updateSet);
// 更新
users.updateOne(filter , update);
// クライントを閉じる
client.close();
}
}
更新后的收藏品数据
更新许多
更新文档的指南:https://docs.mongodb.com/manual/tutorial/update-documents/
简单替换一个
如果要替换文档的全部数据而不是部分更新,可以使用replaceOne方法。
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document update = new Document();
update.append("lastName", "鈴木");
update.append("updateDate", new Date());
// 差し替え
users.replaceOne(filter, update);
在处理将DB中搜索的文档作为替换数据时需要注意一点,这可能会导致错误。
错误示例:
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
// 差し替え(違う_idなので、エラーになる。同じ_idなら問題ありません。)
users.replaceOne(Filters.eq("_id", new ObjectId("5d859c77b64c3286a4d29d90")), user);
如果不同的_id,则删除用户的_id字段可能会解决错误。
如果存在版本数据,则可以考虑使用replaceOne方法。
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
user.remove("_id");
删除
删除图像
删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// ユーザーの属性をセット
Document user = new Document();
user.append("lastName", "tanaka");
user.append("firstName", "ichiro");
user.append("gender", "男性");
// DBにインサート
users.insertOne(user);
// クライントを閉じる
client.close();
}
}

多個插入
List<Document> userList = new ArrayList<Document>();
Document user1 = new Document();
user1.append("lastName", "tanaka");
Document user2 = new Document();
user2.append("lastName", "yamada");
userList.add(user1);
userList.add(user2);
// リストをDBにインサート
users.insertMany(userList);
阅读 (Yuè dú)
搜索图像
找一个
可以使用类似于 `db.collection.find(filter).first()` 的方式来获取第一条数据。如果想通过 _id 进行搜索,可以使用 `first` 方法非常方便。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// _idで検索する
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
if (user != null) {
System.out.println("lastName:" + user.getString("lastName"));
}
// クライントを閉じる
client.close();
}
}
努力工作
姓:田中
查找列表
搜索示例
// 姓で複数人を検索する
MongoCursor<Document> cursor = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).cursor();
while (cursor.hasNext()) {
Document user = (Document) cursor.next();
// do something
}
// Cursorではなく、リストに変換しても便利です。
// List<Document> documents = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).into(new ArrayList<Document>());
排序、跳过、限制等
Document sort = new Document();
sort.append("updateDate", -1);
MongoCursor<Document> cursor = users.find(Filters.in("lastName", "tanaka", "yamada")).sort(sort).skip(20).limit(10).cursor();
更新
更新的图片 de
第一次更新
使用updateOne方法更新一条数据。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Date;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document updateSet = new Document();
updateSet.append("lastName", "鈴木");
updateSet.append("updateDate", new Date());
// updateオブジェクト
Document update = new Document();
update.append("$set", updateSet);
// 更新
users.updateOne(filter , update);
// クライントを閉じる
client.close();
}
}
更新后的收藏品数据
更新许多
更新文档的指南:https://docs.mongodb.com/manual/tutorial/update-documents/
简单替换一个
如果要替换文档的全部数据而不是部分更新,可以使用replaceOne方法。
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document update = new Document();
update.append("lastName", "鈴木");
update.append("updateDate", new Date());
// 差し替え
users.replaceOne(filter, update);
在处理将DB中搜索的文档作为替换数据时需要注意一点,这可能会导致错误。
错误示例:
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
// 差し替え(違う_idなので、エラーになる。同じ_idなら問題ありません。)
users.replaceOne(Filters.eq("_id", new ObjectId("5d859c77b64c3286a4d29d90")), user);
如果不同的_id,则删除用户的_id字段可能会解决错误。
如果存在版本数据,则可以考虑使用replaceOne方法。
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
user.remove("_id");
删除
删除图像
删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above
List<Document> userList = new ArrayList<Document>();
Document user1 = new Document();
user1.append("lastName", "tanaka");
Document user2 = new Document();
user2.append("lastName", "yamada");
userList.add(user1);
userList.add(user2);
// リストをDBにインサート
users.insertMany(userList);
搜索图像
找一个
可以使用类似于 `db.collection.find(filter).first()` 的方式来获取第一条数据。如果想通过 _id 进行搜索,可以使用 `first` 方法非常方便。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// _idで検索する
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
if (user != null) {
System.out.println("lastName:" + user.getString("lastName"));
}
// クライントを閉じる
client.close();
}
}
努力工作
姓:田中
查找列表
搜索示例
// 姓で複数人を検索する
MongoCursor<Document> cursor = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).cursor();
while (cursor.hasNext()) {
Document user = (Document) cursor.next();
// do something
}
// Cursorではなく、リストに変換しても便利です。
// List<Document> documents = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).into(new ArrayList<Document>());
排序、跳过、限制等
Document sort = new Document();
sort.append("updateDate", -1);
MongoCursor<Document> cursor = users.find(Filters.in("lastName", "tanaka", "yamada")).sort(sort).skip(20).limit(10).cursor();
更新
更新的图片 de
第一次更新
使用updateOne方法更新一条数据。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Date;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document updateSet = new Document();
updateSet.append("lastName", "鈴木");
updateSet.append("updateDate", new Date());
// updateオブジェクト
Document update = new Document();
update.append("$set", updateSet);
// 更新
users.updateOne(filter , update);
// クライントを閉じる
client.close();
}
}
更新后的收藏品数据
更新许多
更新文档的指南:https://docs.mongodb.com/manual/tutorial/update-documents/
简单替换一个
如果要替换文档的全部数据而不是部分更新,可以使用replaceOne方法。
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document update = new Document();
update.append("lastName", "鈴木");
update.append("updateDate", new Date());
// 差し替え
users.replaceOne(filter, update);
在处理将DB中搜索的文档作为替换数据时需要注意一点,这可能会导致错误。
错误示例:
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
// 差し替え(違う_idなので、エラーになる。同じ_idなら問題ありません。)
users.replaceOne(Filters.eq("_id", new ObjectId("5d859c77b64c3286a4d29d90")), user);
如果不同的_id,则删除用户的_id字段可能会解决错误。
如果存在版本数据,则可以考虑使用replaceOne方法。
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
user.remove("_id");
删除
删除图像
删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above

找一个
可以使用类似于 `db.collection.find(filter).first()` 的方式来获取第一条数据。如果想通过 _id 进行搜索,可以使用 `first` 方法非常方便。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// _idで検索する
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
if (user != null) {
System.out.println("lastName:" + user.getString("lastName"));
}
// クライントを閉じる
client.close();
}
}
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// _idで検索する
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
if (user != null) {
System.out.println("lastName:" + user.getString("lastName"));
}
// クライントを閉じる
client.close();
}
}
努力工作
姓:田中
查找列表
搜索示例
// 姓で複数人を検索する
MongoCursor<Document> cursor = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).cursor();
while (cursor.hasNext()) {
Document user = (Document) cursor.next();
// do something
}
// Cursorではなく、リストに変換しても便利です。
// List<Document> documents = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).into(new ArrayList<Document>());
排序、跳过、限制等
Document sort = new Document();
sort.append("updateDate", -1);
MongoCursor<Document> cursor = users.find(Filters.in("lastName", "tanaka", "yamada")).sort(sort).skip(20).limit(10).cursor();
更新
更新的图片 de
第一次更新
使用updateOne方法更新一条数据。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Date;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document updateSet = new Document();
updateSet.append("lastName", "鈴木");
updateSet.append("updateDate", new Date());
// updateオブジェクト
Document update = new Document();
update.append("$set", updateSet);
// 更新
users.updateOne(filter , update);
// クライントを閉じる
client.close();
}
}
更新后的收藏品数据
更新许多
更新文档的指南:https://docs.mongodb.com/manual/tutorial/update-documents/
简单替换一个
如果要替换文档的全部数据而不是部分更新,可以使用replaceOne方法。
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document update = new Document();
update.append("lastName", "鈴木");
update.append("updateDate", new Date());
// 差し替え
users.replaceOne(filter, update);
在处理将DB中搜索的文档作为替换数据时需要注意一点,这可能会导致错误。
错误示例:
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
// 差し替え(違う_idなので、エラーになる。同じ_idなら問題ありません。)
users.replaceOne(Filters.eq("_id", new ObjectId("5d859c77b64c3286a4d29d90")), user);
如果不同的_id,则删除用户的_id字段可能会解决错误。
如果存在版本数据,则可以考虑使用replaceOne方法。
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
user.remove("_id");
删除
删除图像
删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above

搜索示例
// 姓で複数人を検索する
MongoCursor<Document> cursor = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).cursor();
while (cursor.hasNext()) {
Document user = (Document) cursor.next();
// do something
}
// Cursorではなく、リストに変換しても便利です。
// List<Document> documents = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).into(new ArrayList<Document>());
排序、跳过、限制等
Document sort = new Document();
sort.append("updateDate", -1);
MongoCursor<Document> cursor = users.find(Filters.in("lastName", "tanaka", "yamada")).sort(sort).skip(20).limit(10).cursor();
更新
更新的图片 de
第一次更新
使用updateOne方法更新一条数据。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Date;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document updateSet = new Document();
updateSet.append("lastName", "鈴木");
updateSet.append("updateDate", new Date());
// updateオブジェクト
Document update = new Document();
update.append("$set", updateSet);
// 更新
users.updateOne(filter , update);
// クライントを閉じる
client.close();
}
}
更新后的收藏品数据
更新许多
更新文档的指南:https://docs.mongodb.com/manual/tutorial/update-documents/
简单替换一个
如果要替换文档的全部数据而不是部分更新,可以使用replaceOne方法。
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document update = new Document();
update.append("lastName", "鈴木");
update.append("updateDate", new Date());
// 差し替え
users.replaceOne(filter, update);
在处理将DB中搜索的文档作为替换数据时需要注意一点,这可能会导致错误。
错误示例:
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
// 差し替え(違う_idなので、エラーになる。同じ_idなら問題ありません。)
users.replaceOne(Filters.eq("_id", new ObjectId("5d859c77b64c3286a4d29d90")), user);
如果不同的_id,则删除用户的_id字段可能会解决错误。
如果存在版本数据,则可以考虑使用replaceOne方法。
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
user.remove("_id");
删除
删除图像
删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above
// 姓で複数人を検索する
MongoCursor<Document> cursor = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).cursor();
while (cursor.hasNext()) {
Document user = (Document) cursor.next();
// do something
}
// Cursorではなく、リストに変換しても便利です。
// List<Document> documents = users.find(Filters.or(Filters.in("lastName", "tanaka", "yamada"))).into(new ArrayList<Document>());
Document sort = new Document();
sort.append("updateDate", -1);
MongoCursor<Document> cursor = users.find(Filters.in("lastName", "tanaka", "yamada")).sort(sort).skip(20).limit(10).cursor();
更新
更新的图片 de
第一次更新
使用updateOne方法更新一条数据。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Date;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document updateSet = new Document();
updateSet.append("lastName", "鈴木");
updateSet.append("updateDate", new Date());
// updateオブジェクト
Document update = new Document();
update.append("$set", updateSet);
// 更新
users.updateOne(filter , update);
// クライントを閉じる
client.close();
}
}
更新后的收藏品数据
更新许多
更新文档的指南:https://docs.mongodb.com/manual/tutorial/update-documents/
简单替换一个
如果要替换文档的全部数据而不是部分更新,可以使用replaceOne方法。
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document update = new Document();
update.append("lastName", "鈴木");
update.append("updateDate", new Date());
// 差し替え
users.replaceOne(filter, update);
在处理将DB中搜索的文档作为替换数据时需要注意一点,这可能会导致错误。
错误示例:
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
// 差し替え(違う_idなので、エラーになる。同じ_idなら問題ありません。)
users.replaceOne(Filters.eq("_id", new ObjectId("5d859c77b64c3286a4d29d90")), user);
如果不同的_id,则删除用户的_id字段可能会解决错误。
如果存在版本数据,则可以考虑使用replaceOne方法。
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
user.remove("_id");
删除
删除图像
删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above

第一次更新
使用updateOne方法更新一条数据。
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Date;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document updateSet = new Document();
updateSet.append("lastName", "鈴木");
updateSet.append("updateDate", new Date());
// updateオブジェクト
Document update = new Document();
update.append("$set", updateSet);
// 更新
users.updateOne(filter , update);
// クライントを閉じる
client.close();
}
}
更新后的收藏品数据
更新许多
更新文档的指南:https://docs.mongodb.com/manual/tutorial/update-documents/
简单替换一个
如果要替换文档的全部数据而不是部分更新,可以使用replaceOne方法。
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document update = new Document();
update.append("lastName", "鈴木");
update.append("updateDate", new Date());
// 差し替え
users.replaceOne(filter, update);
在处理将DB中搜索的文档作为替换数据时需要注意一点,这可能会导致错误。
错误示例:
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
// 差し替え(違う_idなので、エラーになる。同じ_idなら問題ありません。)
users.replaceOne(Filters.eq("_id", new ObjectId("5d859c77b64c3286a4d29d90")), user);
如果不同的_id,则删除用户的_id字段可能会解决错误。
如果存在版本数据,则可以考虑使用replaceOne方法。
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
user.remove("_id");
删除
删除图像
删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above
package mongodb;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Date;
import org.bson.Document;
import org.bson.types.ObjectId;
public class MongoDBTest {
public static void main(String[] args) {
// MongoDBクライントを生成する
MongoClient client = MongoClients.create("mongodb://localhost:27017");
// DBオブジェクトを取得する
MongoDatabase db = client.getDatabase("mydb");
// ユーザーコレクションを取得する
MongoCollection<Document> users = db.getCollection("users");
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document updateSet = new Document();
updateSet.append("lastName", "鈴木");
updateSet.append("updateDate", new Date());
// updateオブジェクト
Document update = new Document();
update.append("$set", updateSet);
// 更新
users.updateOne(filter , update);
// クライントを閉じる
client.close();
}
}
更新后的收藏品数据
更新许多
更新文档的指南:https://docs.mongodb.com/manual/tutorial/update-documents/
简单替换一个
如果要替换文档的全部数据而不是部分更新,可以使用replaceOne方法。
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document update = new Document();
update.append("lastName", "鈴木");
update.append("updateDate", new Date());
// 差し替え
users.replaceOne(filter, update);
在处理将DB中搜索的文档作为替换数据时需要注意一点,这可能会导致错误。
错误示例:
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
// 差し替え(違う_idなので、エラーになる。同じ_idなら問題ありません。)
users.replaceOne(Filters.eq("_id", new ObjectId("5d859c77b64c3286a4d29d90")), user);
如果不同的_id,则删除用户的_id字段可能会解决错误。
如果存在版本数据,则可以考虑使用replaceOne方法。
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
user.remove("_id");
删除
删除图像
删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above

更新许多
更新文档的指南:https://docs.mongodb.com/manual/tutorial/update-documents/
简单替换一个
如果要替换文档的全部数据而不是部分更新,可以使用replaceOne方法。
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document update = new Document();
update.append("lastName", "鈴木");
update.append("updateDate", new Date());
// 差し替え
users.replaceOne(filter, update);
在处理将DB中搜索的文档作为替换数据时需要注意一点,这可能会导致错误。
错误示例:
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
// 差し替え(違う_idなので、エラーになる。同じ_idなら問題ありません。)
users.replaceOne(Filters.eq("_id", new ObjectId("5d859c77b64c3286a4d29d90")), user);
如果不同的_id,则删除用户的_id字段可能会解决错误。
如果存在版本数据,则可以考虑使用replaceOne方法。
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
user.remove("_id");
删除
删除图像
删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above

简单替换一个
如果要替换文档的全部数据而不是部分更新,可以使用replaceOne方法。
// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document update = new Document();
update.append("lastName", "鈴木");
update.append("updateDate", new Date());
// 差し替え
users.replaceOne(filter, update);
在处理将DB中搜索的文档作为替换数据时需要注意一点,这可能会导致错误。
错误示例:
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
// 差し替え(違う_idなので、エラーになる。同じ_idなら問題ありません。)
users.replaceOne(Filters.eq("_id", new ObjectId("5d859c77b64c3286a4d29d90")), user);
如果不同的_id,则删除用户的_id字段可能会解决错误。
如果存在版本数据,则可以考虑使用replaceOne方法。
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
user.remove("_id");
删除
删除图像
删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above

// 更新条件
Document filter = new Document();
filter.append("_id", new ObjectId("5d858b36c491de05900c4e32"));
// 更新データ
Document update = new Document();
update.append("lastName", "鈴木");
update.append("updateDate", new Date());
// 差し替え
users.replaceOne(filter, update);
在处理将DB中搜索的文档作为替换数据时需要注意一点,这可能会导致错误。
错误示例:
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
// 差し替え(違う_idなので、エラーになる。同じ_idなら問題ありません。)
users.replaceOne(Filters.eq("_id", new ObjectId("5d859c77b64c3286a4d29d90")), user);
如果不同的_id,则删除用户的_id字段可能会解决错误。
如果存在版本数据,则可以考虑使用replaceOne方法。
// DBからデータ取得
Document user = users.find(Filters.eq("_id", new ObjectId("5d858b36c491de05900c4e32"))).first();
user.append("memo", "new user");
user.remove("_id");
删除
删除图像
删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above

删除一个使用者删除一个(”_id” = ObjectId(“5d859c77b64c3286a4d29d90”))的记录。
删除多个删除姓为「鈴木」的用户,使用 users.deleteMany(Filters.eq(“lastName”, “鈴木”)) 方法。
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above
删除相关方法
手册链接:https://docs.mongodb.com/manual/
CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above

CRUD(文中的说明):https://docs.mongodb.com/manual/crud/
以上 – Above