显示带文档和子文档示例的 MongoDB 并更新
下面是显示文档和子文档的语法 -
db.yourCollectionName.insertOne( { yourFiledName:yourValue, yourFieldName : [ { yourFiledName1, yourFiledName2, . . . N } ] } );
让我们看一个带文档的示例创建集合 -
> db.demo706.insertOne( ... { ... PortalName: "GameApplication", ... ApplicationConfiguration : [ ... { ... "URL": "jdbc:mysql://127.0.0.1/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://127.0.0.1/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://127.0.0.1/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" } ] }
广告