当前位置:网站首页>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 !
边栏推荐
- Mongodb在node中的使用
- Shell_ 04_ Shell script
- Saw local status change event StatusChangeEvent [timestamp=1644048792587, current=DOWN, previous=UP]
- ~79 Movie card exercise
- 原型链继承
- Error occurred during initialization of VM Could not reserve enough space for object heap
- Usage of insert() in vector
- 登陆验证koa-passport中间件的简单使用
- Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
- Record the error reason
猜你喜欢
Log4j2 major vulnerabilities and Solutions
字节跳动多篇论文入选 CVPR 2021,精选干货都在这里了
Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.
Alibaba cloud server builds SVN version Library
Data config problem: the reference to entity 'useunicode' must end with ';' delimiter.
The QT program compiled on CentOS lacks a MySQL driven solution
Activiti目录(五)驳回、重新发起、取消流程
~83 form introduction
字节跳动2022校招研发提前批宣讲会,同学们最关心的10个问题
How to configure hosts when setting up Eureka
随机推荐
koa中间件
Thank you for your invitation. I'm in the work area. I just handed in the code. I'm an intern in the next ByteDance
面试集锦库
(multiple methods, need to continue to see) 7-11 go deep into the tiger's Den
Alibaba cloud server builds SVN version Library
字节跳动技术新人培训全记录:校招萌新成长指南
Erlang installation
Solr word segmentation analysis
ByteDance 2022 school recruitment R & D advance approval publicity meeting, students' top 10 issues
J'ai traversé le chemin le plus fou, le circuit cérébral d'un programmeur de saut d'octets
Data config problem: the reference to entity 'useunicode' must end with ';' delimiter.
Go language uses the thrift protocol to realize the client and service end reports not enough arguments in call to oprot Writemessagebegin error resolution
Shell_ 04_ Shell script
这116名学生,用3天时间复刻了字节跳动内部真实技术项目
Saw local status change event StatusChangeEvent [timestamp=1644048792587, current=DOWN, previous=UP]
Activiti目录(四)查询代办/已办、审核
「博士毕业一年,我拿下 ACL Best Paper」
The most lost road I have ever walked through is the brain circuit of ByteDance programmers
MySQL digital function
搭建flutter环境入坑集合