MongoDB
 Computer >> コンピューター >  >> プログラミング >> MongoDB

特殊文字を含むコレクションをMongoDBから削除しますか?


_や-などの特殊文字を含むコレクションを削除するには、次の構文を使用する必要があります-

db.getCollection("yourCollectionName").drop();

概念を理解するために、ドキュメントを使用してコレクションを作成しましょう。ドキュメントを使用してコレクションを作成するためのクエリは次のとおりです-

> db.createCollection("_personalInformation");
{ "ok" : 1 }

> db.getCollection('_personalInformation').insertOne({"ClientName":"Chris","ClientCountryName":"US"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9158bb4afe5c1d2279d6b2")
}
> db.getCollection('_personalInformation').insertOne({"ClientName":"Mike","ClientCountryName":"UK"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9158c84afe5c1d2279d6b3")
}
> db.getCollection('_personalInformation').insertOne({"ClientName":"David","ClientCountryName":"AUS"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5c9158d54afe5c1d2279d6b4")
}

find()メソッドを使用して、コレクションのすべてのドキュメントを表示します。クエリは次のとおりです-

> db.getCollection('_personalInformation').find().pretty();

以下は出力です-

{
   "_id" : ObjectId("5c9158bb4afe5c1d2279d6b2"),
   "ClientName" : "Chris",
   "ClientCountryName" : "US"
}
{
   "_id" : ObjectId("5c9158c84afe5c1d2279d6b3"),
   "ClientName" : "Mike",
   "ClientCountryName" : "UK"
}
{
   "_id" : ObjectId("5c9158d54afe5c1d2279d6b4"),
   "ClientName" : "David",
   "ClientCountryName" : "AUS"
}

これは、特殊文字-

を持つMongoDBからコレクションを削除するためのクエリです。
> db.getCollection("_personalInformation").drop();

以下は出力です-

True

結果TRUEは、MongoDBからコレクションを完全に削除したことを示します。


  1. サブコレクションとのMongoDB同時更新?

    更新するには、update()を使用するだけです。 $ push演算子を使用して、指定された値とドット表記を追加して、サブコレクションに到達し、update()内で更新します。 ドキュメントを使用してコレクションを作成しましょう- > db.demo547.insertOne( ... { ...    Name : "Chris", ...    Test : ...    { ...       "FirstTest" : ...   &nbs

  2. MongoDBコレクションから重複を削除するにはどうすればよいですか?

    このために、「 unique:true」を設定します 」つまり、一意の制約を使用し、次の構文のように重複を挿入しないようにします- db.yourCollectionName.ensureIndex({yourFieldName: 1}, {unique: true, dropDups: true}) 上記の構文を理解するために、ドキュメントを使用してコレクションを作成しましょう。ここでは、重複挿入は許可されません- > db.demo604.ensureIndex({FirstName: 1}, {unique: true, dropDups: true});{   &nb