当前位置:网站首页>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 !
边栏推荐
猜你喜欢

MySQL digital function

字节跳动春招攻略:学长学姐笔经面经,还有出题人「锦囊」

MySQL日期函数

100张图训练1小时,照片风格随意变,文末有Demo试玩|SIGGRAPH 2021

Eight part essay that everyone likes

Activit零零碎碎要人命的坑

"One year after graduation, I won ACL best paper"

原型链继承

Alibaba cloud server docker installation mysql5.5

Install docker under windows10 (through Oracle VM VirtualBox)
随机推荐
Error occurred during initialization of VM Could not reserve enough space for object heap
Eight part essay that everyone likes
Alibaba cloud server docker installation mysql5.5
8086 内存
[unsolved]7-14 calculation diagram
IDEA断点调试技巧,多张动图包教包会。
Activiti目录(五)驳回、重新发起、取消流程
Resume of a microservice architecture teacher with 10 years of work experience
登陆验证koa-passport中间件的简单使用
~85 transition
ByteDance 2022 school recruitment R & D advance approval publicity meeting, students' top 10 issues
姚班智班齐上阵,竞赛高手聚一堂,这是什么神仙编程大赛?
一个数10年工作经验的微服务架构老师的简历
8086 分段技术
字节跳动技术面试官现身说法:我最想pick什么样的候选人
控制转移指令
MySQL日期函数
Mongodb learning notes
7-6 sum of combinatorial numbers
Shell_ 02_ Text three swordsman