
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Display MongoDB with document and subdocument example and update
Following is the syntax showing document and subdocument −
db.yourCollectionName.insertOne( { yourFiledName:yourValue, yourFieldName : [ { yourFiledName1, yourFiledName2, . . . N } ] } );
Let us see an example create a collection with documents −
> 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") }
Display all documents from a collection with the help of find() method −
> db.demo706.find();
This will produce the following output −
{ "_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" } ] }
Following is the query to update −
> db.demo706.update({PortalName: "WebMyBusinessApplication"},{$set:{"PortalName":"OnlineCustomerTracker"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo706.find();
This will produce the following output −
{ "_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" } ] }
- Related Articles
- Update a MongoDB document with Student Id and Name
- MongoDB query to remove subdocument from document?
- Update a specific MongoDB document in array with $set and positional $ operator?
- Display the undefined and exact MongoDB document records
- MongoDB query to find and return subdocument with criteria?
- MongoDB query to update nested document
- Update two separate arrays in a document with one update call in MongoDB?
- How do I get email-id from a MongoDB document and display with print()
- Combine update and query parts to form the upserted document in MongoDB?
- MongoDB findOneAndUpdate() to update a single document
- Update only a single document in MongoDB
- MongoDB query to update the nested document?
- How to reach subdata in MongoDB and display a particular document?
- Update array in MongoDB document by variable index?
- How to update document with marks value in MongoDB for student David

Advertisements