当前位置:网站首页>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 !
边栏推荐
- Shell_ 03_ environment variable
- 这116名学生,用3天时间复刻了字节跳动内部真实技术项目
- ~87 animation
- js垃圾回收机制和内存泄漏
- 在 vi 编辑器中的命令模式下,删除当前光标处的字符使用 __ 命 令。
- 8086 CPU 内部结构
- string. How to choose h and string and CString
- The difference between URI and URL
- Go language uses the thrift protocol to realize the client and service end reports not enough arguments in call to oprot Writemessagebegin error resolution
- Notes on how the network is connected
猜你喜欢

Mongodb learning notes

Erlang installation

When it comes to Google i/o, this is how ByteDance is applied to flutter

~86m rabbit practice

Activit零零碎碎要人命的坑

逻辑运算指令

字节跳动2022校招研发提前批宣讲会,同学们最关心的10个问题

Activiti目录(五)驳回、重新发起、取消流程

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

~74 JD top navigation bar exercise
随机推荐
After idea installs the plug-in, restart the plug-in and disappear
我走過最迷的路,是字節跳動程序員的腦回路
数据传送指令
字节跳动海外技术团队再夺冠:高清视频编码已获17项第一
[graduation project] QT from introduction to practice: realize imitation of QQ communication, which is also the last blog post in school.
was unable to send heartbeat
唯有学C不负众望 TOP1环境配置
8086 segmentation technology
Activiti目录(三)部署流程、发起流程
这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
7-8 likes (need to continue to improve)
@RequestMapping、@GetMapping
The 116 students spent three days reproducing the ByteDance internal real technology project
js垃圾回收机制和内存泄漏
字节跳动春招攻略:学长学姐笔经面经,还有出题人「锦囊」
~79 Movie card exercise
MySQL string function
Data config problem: the reference to entity 'useunicode' must end with ';' delimiter.
MySQL日期函数
Mongodb在node中的使用