createdCollectionAutomatically在MongoDB中是什麼意思?


如果集合不存在,則MongoDB會在索引部分中建立一個集合 createdCollectionAutomatically 告知操作建立了一個集合。

對於我們的示例,讓我們建立一個帶索引的集合 −

> db.createCollectionDemo.createIndex({"ClientCountryName":1});

這將產生以下輸出 −

{
   "createdCollectionAutomatically" : true,
   "numIndexesBefore" : 1,
   "numIndexesAfter" : 2,
   "ok" : 1
}

讓我們建立一個帶有文件的集合 −

> db.createCollectionDemo.insertOne({"ClientName":"Larry","ClientCountryName":"US"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cd2950be3526dbddbbfb612")
}

以下是使用`find()`方法顯示集合中所有文件的查詢 −

> db.createCollectionDemo.find().pretty();

這將產生以下輸出 −

{
   "_id" : ObjectId("5cd2950be3526dbddbbfb612"),
   "ClientName" : "Larry",
   "ClientCountryName" : "US"
}

更新於:30-Jul-2019

182次瀏覽

開啟你的職業

完成課程,獲得認證

開始
廣告