從以 _ 起始到查詢 MongoDB 集合


對於從 _ 開始的 MongoDB 集合,以下為其語法 −

db.createCollection(‘_yourCollectionName’);

使用以下語法插入查詢 −

db.getCollection('_yourCollectionName').insertOne({"yourFieldName1":"yourValue1","yourFieldName2":yourValue2,............N});

首先,我們建立一個帶有文件的集合 −

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

>db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"John","StudentAge":23});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ccfb4a6140b992277dae0e4")
}

>db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"Carol","StudentAge":21});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ccfb4af140b992277dae0e5")
}

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

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

這將產生以下輸出 −

{
   "_id" : ObjectId("5ccfb4a6140b992277dae0e4"),
   "StudentFirstName" : "John",
   "StudentAge" : 23
}
{
   "_id" : ObjectId("5ccfb4af140b992277dae0e5"),
   "StudentFirstName" : "Carol",
   "StudentAge" : 21
}

更新時間: 2019 年 7 月 30 日

171 次瀏覽

開啟你的 職業

完成課程獲得認證

開始
廣告