ต่อไปนี้เป็นรูปแบบการแสดงเอกสารและเอกสารย่อย -
db.yourCollectionName.insertOne( { yourFiledName:yourValue, yourFieldName : [ { yourFiledName1, yourFiledName2, . . . N } ] } );
ให้เราดูตัวอย่างการสร้างคอลเลกชันที่มีเอกสาร -
> db.demo706.insertOne( ... { ... PortalName: "GameApplication", ... ApplicationConfiguration : [ ... { ... "URL": "jdbc:mysql://localhost/customer_tracker?autoReconnect=true", ... "USERNAME": "root", ... "PASSWORD": "root" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea6f557551299a9f98c93c6") } > > db.demo706.insertOne( ... { ... PortalName: "WebMyBusinessApplication", ... ApplicationConfiguration : [ ... { ... "URL": "jdbc:oracle:thin:@localhost:1521:xe", ... "USERNAME": "App", ... "PASSWORD": "App" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea6f558551299a9f98c93c7") }
แสดงเอกสารทั้งหมดจากคอลเล็กชันโดยใช้วิธี find() -
> db.demo706.find();
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{ "_id" : ObjectId("5ea6f557551299a9f98c93c6"), "PortalName" : "GameApplication", "ApplicationConfiguration" : [ { "URL" : "jdbc:mysql://localhost/customer_tracker?autoReconnect=true", "USERNAME" : "root", "PASSWORD" : "root" } ] } { "_id" : ObjectId("5ea6f558551299a9f98c93c7"), "PortalName" : "WebMyBusinessApplication", "ApplicationConfiguration" : [ { "URL" : "jdbc:oracle:thin:@localhost:1521:xe", "USERNAME" : "App", "PASSWORD" : "App" } ] }
ต่อไปนี้เป็นแบบสอบถามที่จะปรับปรุง -
> db.demo706.update({PortalName: "WebMyBusinessApplication"},{$set:{"PortalName":"OnlineCustomerTracker"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
แสดงเอกสารทั้งหมดจากคอลเล็กชันโดยใช้วิธี find() -
> db.demo706.find();
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{ "_id" : ObjectId("5ea6f557551299a9f98c93c6"), "PortalName" : "GameApplication", "ApplicationConfiguration" : [ { "URL" : "jdbc:mysql://localhost/customer_tracker?autoReconnect=true", "USERNAME" : "root", "PASSWORD" : "root" } ] } { "_id" : ObjectId("5ea6f558551299a9f98c93c7"), "PortalName" : "OnlineCustomerTracker", "ApplicationConfiguration" : [ { "URL" : "jdbc:oracle:thin:@localhost:1521:xe", "USERNAME" : "App", "PASSWORD" : "App" } ] }