The original:

  1. Download the mongodb installation package at www.mongodb.com/download-ce…
  2. The installation
  3. After the installation is complete, create a DB folder under the data folder
  4. Add environment variables: Copy the bin folder in the installation directory, right-click my computer, and paste the bin file path in the environment variable using a semicolon

    Separate environment variables
  5. Open the console and enter: mongod to check whether the installation is successful
  6. Connect to the database using the mongo command

The db operation:

Create a new database named User

Use User------ Database name (Switch the current database, or create a new database if not)Copy the code
Show DBS ------ Displays all databasesCopy the code
Db. User. Insert ({" name ":" ABC "}) -- -- -- -- -- insert a name for the 'ABC'Copy the code

Collection: collection

Db.createcollection (‘ collection name ‘); Create a collection

Db.collection name. drop() drops a collection

Show Collections looks at all collections

Every database has to have collections, otherwise you’re creating temporary data

The document operation:

Insert document:

db.col.insert({

“email” : “admin”,

“password” : “root”

})

Find documents:

Db.col. findCopy the code

Update document:

db.col.update({
  "email" : "admin"
}, {
  $set: {
    "email" : "admin1"."password" : "root1"}}, {multi: true
})
Copy the code

Find all values with email admin and update the email to admin1 and password to root1. Multi is set to true to update multiple documents

Overwrite updates:

db.col.save({
  '_id':3.'email': 'abc'
})
Copy the code

Overrides the old value based on the id passed in

Delete document:

db.col.remove({
  '_id':3
})
Copy the code

Delete the item whose ID is 3

db.col.remove({})

Remove all col

Limit the number of items displayed:

Db.col.find ().limit(2) displays the first two items foundCopy the code

Skip the displayed data:

Db.age.find ().skip(2) indicates that the first two items found are skippedCopy the code

Sorting:

Db.col.find ().sort({age:1}) sort by age (-1 is descending)Copy the code

The connection Node

First download the Node plug-in:

npm install mongodb
Copy the code

Then execute the following code:

var MongoClient = require("mongodb").MongoClient;// Get the mongo module
var mongoDB = "mongodb://localhost:27017/";
// Create a database connection
MongoClient.connect(
    mongoDB,
    function (err, db) {
        if (err) {
            throw err;
        }
        console.log("Connection successful!");
        db.close();// Disconnect the connection});Copy the code

The Node query:

var MongoClient = require("mongodb").MongoClient;
var mongoDB = "mongodb://localhost:27017/";
MongoClient.connect(
    mongoDB,{useNewUrlParser: true},
    function (err, db) {
        if (err) {
            throw err;
        }
        console.log("Connection successful!");
        var dball = db.db('UserList');// Database name
        dball.collection('allUser').find({}).toArray(function(err,result) {// Query statement
            if (err) {
                console.log(arr);/ / wrong
                return;
            }
            console.log(result);// Print the result of the query.db.close(); }); });Copy the code

Insert the Node:

var MongoClient = require("mongodb").MongoClient;
var mongoDB = "mongodb://localhost:27017/";
MongoClient.connect(
    mongoDB,{useNewUrlParser: true},
    function (err, db) {
        if (err) {
            throw err;
        }
        console.log("Connection successful!");
        var dball = db.db('UserList');      
        / / add
        dball.collection("allUser").insert([{// Insert two items into the array
            email: '12345'.password:'54321'
        }, {
            email: 'root'.password:'root'}].function (err, result) {
            if (err) {
                console.log('Error:' + err);
                return;
            }
            console.log(result)// Return the insert result
        })
        db.close();// Close the database connection});Copy the code

The Node update:

var MongoClient = require("mongodb").MongoClient;
var mongoDB = "mongodb://localhost:27017/";
MongoClient.connect(
    mongoDB,{useNewUrlParser: true},
    function (err, db) {
        if (err) {
            throw err;
        }
        console.log("Connection successful!");
        var dball = db.db('UserList');
        // Update database
        dball.collection("allUser").update({// change the email to 12345 and password to 12345
            email: 'abcde'
        }, {$set: {email: '12345'.password:'12345'
        }}, function (err) {
            if (err) {
                console.log('Error:' + err);
                return; } }) db.close(); });Copy the code

The Node to delete:

var MongoClient = require("mongodb").MongoClient;
var mongoDB = "mongodb://localhost:27017/";
MongoClient.connect(
    mongoDB,{useNewUrlParser: true},
    function (err, db) {
        if (err) {
            throw err;
        }
        console.log("Connection successful!");
        var dball = db.db('UserList');
        / / delete
        dball.collection("allUser").remove({
        // Delete the item whose email is 12345
            email: '12345'
        }, function (err) {
            if (err) {
                console.log('Error:' + err);
                return; } }) db.close(); });Copy the code