Based on some

The database is persisted to the database, to prevent the loss of information or data caused by computer restart, because the program is running in memory

Non-relational databases (NoSQL) are opposed to SQL because many programmers do not like this standardization of SQL because standardization limits innovation to a certain extent.

MongoDB is a document database in a non-relational database for rapid development of Internet applications. MongoDB stores binary JSON, which we can simply consider JSON;

We put collections in the database, we put documents in the collection, we cruD the documents, we don’t have to create collections or databases manually

instruction explain
Uer < database name > Using a database
db Represents the current database
show dbs Display all databases
show collections Displays all collections in the database
Db.< set name >. Insert ({JSON}) Insert the JSON document into the collection
Db. < collection name >.find({attribute name :” value “, attribute name :” value “}) Query the documents in this collection, the relationship between these attributes is AND, return the documents that conform to these attributes
Db.< collection name >.findone ({attribute name :” value “, attribute name :” value “}) Returns the first document found that matches the criteria, and the rest as found in find ()
Db.< collection name >.find().count() View the total number of documents returned
Db. Set the < name >. Update ({age: “19”}, {name: “liuliu”, the age: “18”, school: “tjut”}) The first argument is the condition, and the second argument is the replacement why (Not recommended)
Db.< set name >. Update ({age:”19″},{$set:{age:”18″}}) To modify a property (It is recommended to use)
Db.< collection name >.remove({}) Delete the document
Db.< set name >.drop() Delete the collection
db.student.find({}).sort({age:1}) Sort query results by age. 1 indicates ascending order and -1 indicates descending order

ps:

1. The content in <> indicates that this is a name

2. By default, update the first document that meets the criteria. Setting multi to true allows you to change multiple documents, and updateMany () changes all documents that meet the criteria.

3. In the actual development, we seldom use the operation of deleting database documents. The general practice is to set a field to indicate whether the document is deleted. Then combine the value of flag to query.

4. Inline queries must be quoted eg: db.user.find({“hobby.cities”:” Beijing “}).

5. We do not execute queries without conditions. Limit (n) is used to limit the number of entries to n. .skip(n) To skip a specified amount of data.

Relationships between documents

One on one, one on many, many on many

projection

Db.student. find({},{name:1}) retrits the fields you want to display, not all of them. For example, if the second argument to find passes a name, the name of all the documents that meet the criteria will be retrieved.

Mongoose (Object Document Model Library)

Mongoose is a module in Node that manipulates the mongoDB database. Map documents in the database to objects in NodeJS. In Mongoose, Schema is a constraint, such as the type of value, how many key-value pairs a document has. Model: A Model corresponds to the collection in the database, Document corresponds to the Document in the database.

Code to connect to the database:

  const mongoose = require("mongoose");
 mongoose.connect("mongodb://localhost/student", {useNewUrlParser:true.useUnifiedTopology: true});
 mongoose.connection.once("open".function(){
    console.log("Database connection setup");
 });
 mongoose.connection.once("close".function(){
     console.log("Database connection down");
 });
let Schema = mongoose.Schema;
let stuSchema = new Schema({
        name:String.age:Number.address: {type:String.default:Tianjin University of Technology}})// A Model is equivalent to a collection in the database
 let stuModel = mongoose.model("tjutStudents",stuSchema); 
// stuModel.create({
// name:"zh",
// age:18,
// address:" Nankai University"
/ /})
stuModel.find({name:"zh"},function(err,docs){
    // docs is the result of the query, which is returned as an array
    let j = JSON.stringify(docs)
    console.log(typeof docs);
    console.log(typeof j);
});
/ / stuModel. UpdateMany ({name: "useful"}, {$set: {address: "Beijing university of posts and telecommunications"}}, function () {
// console.log(" Data changed successfully ");
// })

// stuModel.countDocuments({},function(err,count){
// console.log(count);
// })
 // In general, mongoDB databases only need to be connected once and will not be disconnected easily
// mongoose.disconnect();
Copy the code