
- 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
Get attribute list from MongoDB object?
To get attribute list from MongoDB object, you can use for loop to extract key and value for document. Let us create a collection with documents −
>db.getAttributeListDemo.insertOne({"StudentId":101,"StudentName":"John","StudentAdmissi onDate":new ISODate('2019-01-12'),"StudentSUbjects":["MongoDB","Java","MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdfcc9ac184d684e3fa269") }
Display all documents from a collection with the help of find() method −
> db.getAttributeListDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cbdfcc9ac184d684e3fa269"), "StudentId" : 101, "StudentName" : "John", "StudentAdmissionDate" : ISODate("2019-01-12T00:00:00Z"), "StudentSUbjects" : [ "MongoDB", "Java", "MySQL" ] }
Following is the query to get attribute list from MongoDB object −
> var myDocument = db.getAttributeListDemo.findOne(); > for (myKey in myDocument) { ... var originalValue = myDocument[myKey]; ... print(myKey + "(" + typeof(originalValue ) + "): " + originalValue ) };
This will produce the following output −
_id(object): 5cbdfcc9ac184d684e3fa269 StudentId(number): 101 StudentName(string): John StudentAdmissionDate(object): Sat Jan 12 2019 05:30:00 GMT+0530 (India Standard Time) StudentSUbjects(object): MongoDB,Java,MySQL
- Related Articles
- How to get items from an object array in MongoDB?
- Get MongoDB documents with max attribute per group in a collection?
- Get Random record from MongoDB?
- How to get a specific object from array of objects inside specific MongoDB document?
- Remove object from array in MongoDB?
- How to get attribute of element from Selenium?
- How to get a saved object in MongoDB?
- Unset an attribute from a single array element in MongoDB?
- HTML list Attribute
- How to get values greater than a specific value from an embedded list in MongoDB?
- How to create a new object and get its saved object in MongoDB?
- Get value of any attribute from XML data in JavaScript?
- How to remove object from array in MongoDB?
- Getting distinct values from object array in MongoDB?
- Get at least one match in list querying with MongoDB?

Advertisements