Node+MongoDB operation database

1. Database (MongoDB) connection

     (1) Install the application generator express

            npm    install    express-generator    -g

    (2) Create an application

            express application name

    (3) Install the mongodb module

            npm    install    mongodb

      (4) Convert page rendering type to HTML

            a. Load ejs

                1)npm    install    ejs

            b.app.js configuration

                1)app.set('view',path.join(__dirname,'views'));

                2)  app.engine('html',require('ejs').renderFile);

                3)  app.set('view    engine','html');

            

           Convert the red box to

            

            c. Change the suffix of the jade file in the views file under the application file to html

        (5) Database connection

                

                a. Enter npm start in CMD under the application folder (provided that the MongoDB service is started)

                    ***Won't MongoDB Click here to open the link ***

2. Database (MongoDB) operations

        (1) Create a MongoDB database collection

                

                

After starting the MongoDB service, run the mongodb file path+bin\mongo.exe

            

            After executing show dbs, you can see all the libraries of the service

            show collectionsView all collections

            Green is the newly created library

        (2) Insert document

                

                insertOne can only insert one piece of data      

              

                insertMany inserts multiple pieces of data

                To view the data first switch the library use library name

                

                Then query db.collection name.find()

                

        (3) Query data

            

            db.site.find() or db.site.find().toArray() in CMD

            ************Others are not listed one by one Click here to open the link ****************

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324890217&siteId=291194637