
- 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
MongoDB - how can I access fields in a document?
To access fields in a document, simply use find(). Let us create a collection with documents −
> db.demo565.insertOne( ... { ... id:101, ... Name:"David", ... "CountryName":"US" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e90896739cfeaaf0b97b577") } > > db.demo565.insertOne( ... { ... id:102, ... Name:"Carol", ... "CountryName":"UK" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e90896839cfeaaf0b97b578") } > > db.demo565.insertOne( ... { ... id:103, ... Name:"Sam", ... "CountryName":"AUS" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e90896839cfeaaf0b97b579") }
Display all documents from a collection with the help of find() method −
> db.demo565.find();
This will produce the following output −
{ "_id" : ObjectId("5e90896739cfeaaf0b97b577"), "id" : 101, "Name" : "David", "CountryName" : "US" } { "_id" : ObjectId("5e90896839cfeaaf0b97b578"), "id" : 102, "Name" : "Carol", "CountryName" : "UK" } { "_id" : ObjectId("5e90896839cfeaaf0b97b579"), "id" : 103, "Name" : "Sam", "CountryName" : "AUS" }
Following is the query to access fields −
> db.demo565.find({"Name":"Carol",CountryName:"UK"},{Name:1});
This will produce the following output −
{ "_id" : ObjectId("5e90896839cfeaaf0b97b578"), "Name" : "Carol" }
- Related Articles
- How can I access document properties using Legacy DOM method in JavaScript?
- MongoDB query for fields in embedded document?
- How to query a document in MongoDB comparing fields from an array?
- MongoDB query with fields in the same document?
- Include all existing fields and add new fields to document in MongoDB?
- How to project specific fields from a document inside an array in Mongodb?
- How can I update and increment two fields in one command in MongoDB?
- How do I access subdocuments in MongoDB queries?
- MongoDB query for exact match on multiple document fields
- How do I delete array value from a document in MongoDB?
- How do I remove a string from an array in a MongoDB document?
- How do I index “or” in MongoDB for indexing multiple fields?
- How can I rename a collection in MongoDB?
- How to delete a document in MongoDB?
- How do I work with array fields in MongoDB to match all?

Advertisements