Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15
Building a data model with
MongoDB and Mongoose
Installing MongoDB • MongoDB is also available for Windows, macOS, and Linux. Detailed instructions about all the following options are available in the documentation at https://docs.mongodb.com/manual/administration/i nstall-community. INSTALLING MONGODB ON WINDOWS • Some direct downloads for Windows are available at • https://docs.mongodb.org/manual/installation, depending on which version of Windows you’re running. Using the MongoDB shell to create a MongoDB database and add data STARTING THE MONGODB SHELL • Drop into the shell by running the following line in terminal: $ mongosh • This command should respond in terminal with a few lines confirming The shell version The server and port that it’s connecting to The server version it has connected to MongoDB shell basics • LISTING ALL LOCAL DATABASES > show dbs • This line returns a list of the local MongoDB database names and their sizes. • If you haven’t created any databases at this point, you still see the two default ones, which look something like this: admin 0.000GB local 0.000GB MongoDB shell basics USING A SPECIFIC DATABASE • If you want to use a specific database, such as the default one called local, you can use the use command, like this: > use local • The shell responds with a message along these lines: switched to db local • This message confirms the name of the database the shell has connected to. Basic Commands In the terminal type cmd mongosh You will get a prompt test> Type cmd test>show dbs Basic Commands • To use the database type use dbname
• Here hp is database name which contains
collections, to view collections type the cmd • hp>show collections Basic Commands Insert a document into new collection • db.abc.insertOne({name:"aaa",age:"22"}) • Here abc is a new collection and inserting first document into it Display documents of collection • To see the collections type the cmd Insert a document into the collection • Insert the document db.hpc.insertOne({name:“aaa",roll:“222"}) Insert many documents into the collection
To insert many documents
db.hpc.insertMany([{name:"bbb",roll:"222"}, {name:"ccc",roll:"333"}]) Remove a document from a collection • To remove the document db.hpc.remove({"name":"jjj"}) Delete a collection from the database • To drop the collection • db.COLLECTION_NAME.drop() hp>db.hpc.drop() Update the document in the collection • To update the document db.hpc.update({"name":"HP"},{$set: {"name":"HariPrasad"} })