Installation of MongoDB visualizer and start of server

Install MongoDB and start server

1.Download mongoDB service: (version 3.1.2)

2. Install - > select custom - > install next

3. Create a new empty directory on the local disk. The location should not be too deep. For example, C:\db is used to store data files

4. Run mongoDB service: enter the bin directory of mongoDB installation path, for example: C:\Program Files\MongoDB\Server.1\bin,
Open command line tools, git bash, cmd or powershell

5. Execute mongod --dbpath c://db. As long as there is no error, it means that the service is started successfully. The default port number is 27017. Do not close this window, because the service is started!!!

6. Visualization tools: robo3t or navicat

The difference between relational database and non relational database

Relational database Non relational database
MySql MongoDB
Two dimensional table json
Table structure is relatively fixed More flexible data structure
database database
table collection
row document
col (column) filed (field)
sql statement js api

Use of MongoDB

1. Create a new project and initialize the project

npm init -y // If you have created a new project, omit this step

2. Install mongoose package

npm install mongoose  // Or yarn add mongoose

3. In the project app.js mongoose module is introduced to connect to the database

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/1912', {useNewUrlParser: true});

4. Install HTTP errors package

npm install http-errors // Or yarn add HTTP errors

5. If the connection of the test database running in localhost is successful or there is a connection error, execute the following code
app.js

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log("we're connected!")
});


The above figure shows the successful connection to the database

Deposit

6. If you connect to the database, you can operate the database. If you use mongoose database, you need to start with Schema. The function of Schema is to change non relational database into relational structure, and you can correctly specify which fields are required in the collection

var UserSchema = new mongoose.Schema({
  name: String,
  age: Number,
  password: String
});

7. Get a model based on the Schema, and associate the UserSchema with users

var User = mongoose.model('users', UserSchema);

8. Based on schema and model, get an instance tom

var tom = new User({ name: 'tom', age: 100, password: "123" });
Step 1-6: connect to the database;
Step 7: define a structure UserSchema;
Step 8: associate the structure with the users collection (MySQL like table);
Step 9: get a document (similar to a MySQL record) according to the structure. At this time, the document has not been saved in the database.

10. Save the document (tom) in the database

tom.save((err, tom) => {
  if (err) return console.error(err);
  else console.log(tom)
});

11. Check whether the database is saved successfully

take

12. Query data (document)

User.find({ name: /^tom/ });

13. Query all data

User.find((err, docs) => {
  if (err) return console.error(err)
  else {
    console.log(docs)
  }
})

14. Conditional query

User.find({
  name:'zhangsan'
},(err, docs) => {
  if (err) return console.error(err)
  else {
    console.log(docs)
  }
})

15. greater than, less than, regular

User.find({
  age: { $gte: 19 },  //$gte: >=   $lte: <=  $gt: >  $lt: <
  password: /\d/
}, (err, docs) => {
  if (err) return console.error(err)
  else {
    console.log(docs)
  }
})

16. The second parameter: do not write is equivalent to select *, write means only query this field

User.find({ password: /\d/ },'_id', (err, docs) => {
  if (err) return console.error(err)
  else {
    console.log(docs)
  }
})

17.skip the limit limit

User.find({ password: /\d/ }, '_id', { skip: 1, limit: 1 }, (err, docs) => {
  if (err) return console.error(err)
  else {
    console.log(docs)
  }
})

delete

18.deleteOne()

User.deleteOne({ name: 'zhangsan' }, (err) => {
  if (err) return console.error(err)
  else {
    console.log("deleted")
  }
})

modify

19.{name:'tom'} is a condition

User.updateOne({ name: 'tom' }, { age: 80 }, (err) => {
  if (err) return console.error(err)
  else {
    console.log("updated")
  }
})

Second parameter

User.updateOne({ name: 'tom' }, { age: 80 }, (err, obj) => {
  if (err) return console.error(err)
  else {
    console.log(obj)
  }
})

{n:1,nModified:1,ok:1}

obj.n: --- the number of conditions that can be found
 obj.nModified How many items have been modified according to this condition
 obj.ok: ---- 1 represents modification success, 0 represents modification failure (err is network connection failure, etc.)

Tags: Database Mongoose MongoDB MySQL

Posted on Sat, 13 Jun 2020 03:30:44 -0400 by andy75180