mango DB installation Create Database, collections and Documents
Database, Collection and Documents in MongoDB:
Databases, collections, documents are important parts of MongoDB without them you are not able to store data on the MongoDB server.
A Database contains a collection, and a collection contains documents and the documents contain data in the form of key-value pair, they are related to each other.
Database
In MongoDB, a database contains the collections of documents. One can create multiple databases on the MongoDB server.
C:\Program Files\MongoDB\Server\5.0\data\
C:\Program Files\MongoDB\Server\5.0\log\How to run monoDB?
first of all, set a path in environment variable the open 'CMD' and type mango --version then show any output like version imformation, so thats your mongodb is work.
It gives list of databases that are present in your MongoDB server.
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>
create Database>use database_name
This command actually switches you to the new database if the given name does not exist and if the given name exists, then it will switch you to the existing database. Now at this stage, if you use the show command to see the database list, you will find that your new database is not present in that database list because, in MongoDB, the database is actually created when you start entering data in that database.
> use shreyashdb
switched to db shreyashdb
[{ "id":"1asd13k", "username": "shreyash kolhe", "email": "shreyashkolhe2001@gmail.com", "role":"develper" } ]
Collections are just like tables in relational databases, they also store data, but in the form of
documents. A single database is allowed to store multiple collections.
As we know that MongoDB databases are schemaless, it is not necessary in a collection that
the schema of one document is similar to another document. Or in other words, a single
collection contains different types of documents. Create a collection to store documents.
> db.shreyashdb.insertOne({ name: "ReactJS", type:"Front End", blogpost:100, active:true})
{
"acknowledged" : true,
"insertedId" : ObjectId("61ca837682bfb31fe50836eb")
}
>
Document
In MongoDB, the data records are stored as BSON documents. Here, BSON stands for binary
representation of JSON(JavaScript Object Notation) documents, although BSON contains
more data types as compared to JSON. The document is created using key-value pairs and the
value of the field can be of any BSON type.
check Current active DB>db
shreyashdb
show collection inside DataBase
show collections
check how many documents in collection
> db.shreyashdb.find()
{ "_id" : ObjectId("61ca837682bfb31fe50836eb"), "name" : "ReactJS", "type" : "Front End", "blogpost" : 100, "active" : true }
>
> db.shreyashdb.find().pretty()
{
"_id" : ObjectId("61ca837682bfb31fe50836eb"),
"name" : "ReactJS",
"type" : "Front End",
"blogpost" : 100,
"active" : true
}
Post a Comment