当前位置:网站首页>Use of mongodb in node
Use of mongodb in node
2022-07-06 17:03:00 【Society, you Lei brother, life is hard, don't bend down】
install mongodb
First of all, let's have a look at this database ,MongoDB Is a database based on distributed file storage . from C++ Language writing . For the purpose of WEB Applications provide scalable, high-performance data storage solutions .
Install and configure this database by referring to the official website ! This blog mainly explains the relationship between the database and node How to establish a connection and realize the main functions of adding, deleting, modifying and checking !
install mongoose
We use mongoose This third party package !
$ npm install mongoos
Set the collection structure , The field name is the attribute name in the table structure , The purpose of constraints is to ensure that there is no dirty data !
var Schema = mongoose.Schema;
var userSchame = new Schema({
username: {
type: String,
required: true
},
age: {
type: Number,
required: true
},
mobelphone: {
type: Number
}
})
Connect to database , The connection is test database !
mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true });
Publish the document structure as a model ,mongoose The first uppercase string will be used as the name of the database , And take its lowercase string complex number as a set !
var User = mongoose.model('User', userSchame);
var admin = new User({
username: "gaochi",
age:18,
mobelphone: "12345678"
});
increase
admin.save(function (err, ret) {
if (err) {
console.log("wrong!")
}
else {
console.log(ret);
}
})
Delete
User.remove(
{
name: 'renjialei' },
function (err, ret) {
if (err) {
console.log(" Delete failed ");
} else {
console.log(" Delete successful ");
}
})
Change
User.updateMany({
username: 'renjialei' }, {
age: 17 }, function (err, ret) {
if (err) {
console.log(" Change failed ");
} else {
console.log(" Change succeeded ");
}
})
check
User.find(
// Here is our query criteria !
/* { username: 'gaochi' }, */
function (err, ret) {
if (err) {
console.log('err!');
} else {
console.log(ret);
}
})
Finally, add a few mongodb Simple commands for
mongod // Open database !
mongo // After opening the database , This command can connect the database and the client !
show dbs // Show the database collection name
use (collections)// Enter the database collection , Create if not !
db.(collections).find() // Find the data in the collection !
show collections // Show all collections in the database !
db.(collections).insertOne({
}) // Add data to the database !
Add a very important knowledge point ,mongod Command can be started mongodb Program , You must create a /data/db Folder , Save our data , Instead of downloading mongodb Create a folder under the directory of !mongo The command is used in our project file , Connect the project to the database !
边栏推荐
- Resume of a microservice architecture teacher with 10 years of work experience
- Fdog series (V): use QT to imitate QQ to realize login interface to main interface, function chapter.
- ~81 long table
- Monomer application concept
- ~83 form introduction
- Record the error reason: terminate called after throwing an instance
- 8086 CPU 内部结构
- Eureka single machine construction
- 汇编语言寻址方式
- Yum install XXX reports an error
猜你喜欢

Activiti目录(三)部署流程、发起流程

ByteDance technical Interviewer: what kind of candidate do I want to pick most

~83 form introduction

Erlang installation
![Saw local status change event StatusChangeEvent [timestamp=1644048792587, current=DOWN, previous=UP]](/img/e6/c53ad67ead1793a2acb93c26e8d377.jpg)
Saw local status change event StatusChangeEvent [timestamp=1644048792587, current=DOWN, previous=UP]

「博士毕业一年,我拿下 ACL Best Paper」

Redis standalone startup

Description of project structure configuration of idea

Shell_ 06_ Judgment and circulation

~68 Icon Font introduction
随机推荐
唯有学C不负众望 TOP3 Demo练习
Resume of a microservice architecture teacher with 10 years of work experience
这116名学生,用3天时间复刻了字节跳动内部真实技术项目
Usage of insert() in vector
Activiti目录(四)查询代办/已办、审核
8086 内存
Error: case label `15 'not within a switch statement
服务器端渲染(SSR)和客户端渲染(CSR)的区别
Activiti目录(一)重点介绍
The "advertising maniacs" in this group of programmers turned Tiktok advertisements into ar games
Conception du système de thermomètre numérique DS18B20
DS18B20數字溫度計系統設計
Eureka high availability
~68 Icon Font introduction
"One year after graduation, I won ACL best paper"
TCP的三次握手和四次挥手
MySQL date function
字节跳动技术面试官现身说法:我最想pick什么样的候选人
汇编语言寻址方式
Install docker under windows10 (through Oracle VM VirtualBox)