
- 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 all the column names in a table in MongoDB
In MongoDB, there is no concept of columns since MongoDB is schema-less and does not contain tables. It contains the concept of collections and a collection has different types of documents to store items.
Let us see the syntax −
db.yourCollectionName.insertOne({“YourFieldName”:yourValue, “yourFieldName”:”yourValue”,.......N});
If you want a single record from a collection, you can use findOne() and in order to get all records from the collection, you can use find().
The syntax is as follows −
db.yourCollectionName.findOne(); //Get Single Record db.yourCollectionName.find(); // Get All Record
To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.collectionOnDifferentDocumentDemo.insertOne({"UserName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c953b98749816a0ce933682") } > db.collectionOnDifferentDocumentDemo.insertOne({"UserName":"David","UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c953ba4749816a0ce933683") } > db.collectionOnDifferentDocumentDemo.insertOne({"UserName":"Carol","UserAge":25,"UserCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c953bb8749816a0ce933684") }
Display all documents from a collection with the help of find() method. As discussed above find(), it will return all records.
The query is as follows −
> db.collectionOnDifferentDocumentDemo.find();
The following is the output −
{ "_id" : ObjectId("5c953b98749816a0ce933682"), "UserName" : "Larry" } { "_id" : ObjectId("5c953ba4749816a0ce933683"), "UserName" : "David", "UserAge" : 24 } { "_id" : ObjectId("5c953bb8749816a0ce933684"), "UserName" : "Carol", "UserAge" : 25, "UserCountryName" : "US" }
Display a single record from a collection with the help of the findOne() method. The query is as follows −
> db.collectionOnDifferentDocumentDemo.findOne();
The following is the output −
{ "_id" : ObjectId("5c953b98749816a0ce933682"), "UserName" : "Larry" }
- Related Articles
- Get all fields names in a MongoDB collection?
- Get names of all keys in the MongoDB collection?
- Get table column names in alphabetical order in MySQL?
- How to select all the column names from a table in Laravel?
- What is the syntax in MySQL to get the column names of a table?
- How to get all the column names from a ResultSet using JDBC
- How to get all table names from a database using JDBC?
- Python Pandas – Display all the column names in a DataFrame
- Get table names using SELECT statement in MySQL?
- Get distinct values from a column in MongoDB?
- How to display the column names from a table excluding some in MySQL?
- Make all column names lower case in MySQL with a single query
- Set 'alias' for all the column names in a single MySQL query
- Get the size of all the documents in a MongoDB query?
- Get the sum of a column in all MySQL rows?
