MongoDBのコレクションからドキュメントを削除するにはどうすればよいですか?
MongoDBのコレクションからドキュメントを削除するには、remove()メソッドを使用する必要があります。構文は次のとおりです。
db.yourCollectionName.remove(yourDeleteValue);
ここでは、いくつかのドキュメントを使用してコレクションを作成しましょう。クエリは次のとおりです。
>db.deleteDocuments.insert({"UserId":1,"UserName":"Bob","UserTechnicalSubject":"Introducti on to PL/SQL"});
WriteResult({ "nInserted" : 1 })
>db.deleteDocuments.insert({"UserId":2,"UserName":"Carol","UserTechnicalSubject":"Introduction to MongoDB"});
WriteResult({ "nInserted" : 1 })
>db.deleteDocuments.insert({"UserId":3,"UserName":"John","UserTechnicalSubject":"Introduction to MySQL"});
WriteResult({ "nInserted" : 1 })
>db.deleteDocuments.insert({"UserId":4,"UserName":"Maxwell","UserTechnicalSubject":"Introduction to SQL Server"});
WriteResult({ "nInserted" : 1 }) find()コマンドを使用して、上記で作成したコレクションのすべてのドキュメントを表示できます。クエリは次のとおりです。
> db.deleteDocuments.find().pretty();
出力は次のとおりです。
{
"_id" : ObjectId("5c6aaa9e64f3d70fcc9147ff"),
"UserId" : 1,
"UserName" : "Bob",
"UserTechnicalSubject" : "Introduction to PL/SQL"
}
{
"_id" : ObjectId("5c6aaab464f3d70fcc914800"),
"UserId" : 2,
"UserName" : "Carol",
"UserTechnicalSubject" : "Introduction to MongoDB"
}
{
"_id" : ObjectId("5c6aaac764f3d70fcc914801"),
"UserId" : 3,
"UserName" : "John",
"UserTechnicalSubject" : "Introduction to MySQL"
}
{
"_id" : ObjectId("5c6aaadc64f3d70fcc914802"),
"UserId" : 4,
"UserName" : "Maxwell",
"UserTechnicalSubject" : "Introduction to SQL Server"
} 単一のドキュメントを削除する
コレクションから単一のドキュメントを削除するには、remove()メソッドを使用する必要があります。クエリは次のとおりです。 「UserId:4」のドキュメントを削除するとします:
>db.deleteDocuments.remove({"UserId":4,"UserName":"Maxwell","UserTechnicalSubject":"Int
roduction to SQL Server"});
WriteResult({ "nRemoved" : 2 }) これで、find()コマンドを使用してすべてのドキュメントを表示し、「UserId:4」ドキュメントが正常に削除されたかどうかを確認できます。クエリは次のとおりです。
> db.deleteDocuments.find().pretty();
出力は次のとおりです。
{
"_id" : ObjectId("5c6aaa9e64f3d70fcc9147ff"),
"UserId" : 1,
"UserName" : "Bob",
"UserTechnicalSubject" : "Introduction to PL/SQL"
}
{
"_id" : ObjectId("5c6aaab464f3d70fcc914800"),
"UserId" : 2,
"UserName" : "Carol",
"UserTechnicalSubject" : "Introduction to MongoDB"
}
{
"_id" : ObjectId("5c6aaac764f3d70fcc914801"),
"UserId" : 3,
"UserName" : "John",
"UserTechnicalSubject" : "Introduction to MySQL"
} すべてのドキュメントを削除
すべてのドキュメントを削除するには、次の構文を使用する必要があります。
db.yourCollectionName.remove({ }); クエリは次のとおりです。
> db.deleteDocuments.remove({}); 出力は次のとおりです。
WriteResult({ "nRemoved" : 3 }) コレクション「deleteDocuments」からすべてのドキュメントを削除しました:
-
MongoDBコレクションから重複を削除するにはどうすればよいですか?
このために、「 unique:true」を設定します 」つまり、一意の制約を使用し、次の構文のように重複を挿入しないようにします- db.yourCollectionName.ensureIndex({yourFieldName: 1}, {unique: true, dropDups: true}) 上記の構文を理解するために、ドキュメントを使用してコレクションを作成しましょう。ここでは、重複挿入は許可されません- > db.demo604.ensureIndex({FirstName: 1}, {unique: true, dropDups: true});{ &nb
-
Javaを使用してコレクションから複数のドキュメントを削除するにはどうすればよいですか?
Javaではcom.mongodb.client.MongoCollection インターフェイスはメソッドdeleteMany()を提供します 。この方法を使用すると、コレクションから複数のドキュメントを一度に削除できます。この方法では、削除基準を指定するフィルターに合格する必要があります。 例 import com.mongodb.client.FindIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import java.util.ArrayLi