mongodb数据库的创建,增删改查

4 篇文章 0 订阅
2 篇文章 0 订阅

使用创建数据库

use '数据库名称'  //有就使用,没有就创建

插入,增加数据

db.user.insert({"name":"xjt"})

在这里插入图片描述

展示数据库的表

 show collections

展示表的数据

> db.user.find()
{ "_id" : ObjectId("6121d6122a72a5ac17821a1d"), "name" : "xjt" }

查找具体的数据

{age:{$gte:22,$lte:25}}//大于等于22,小于等于25
> db.user.find()
{ "_id" : ObjectId("6121d9702a72a5ac17821a1f"), "age" : 22 }
{ "_id" : ObjectId("6121d9b92a72a5ac17821a20"), "age" : 25 }
> db.user.find({"age":22})//等于
{ "_id" : ObjectId("6121d9702a72a5ac17821a1f"), "age" : 22 }
> db.user.find({age:{$gt:22}})//大于 {age:{$gte:22}}//大于等于
{ "_id" : ObjectId("6121d9b92a72a5ac17821a20"), "age" : 25 }
> db.user.find({age:{$lt:24}})//小于  {age:{$gle:24}}//小于等于
{ "_id" : ObjectId("6121d9702a72a5ac17821a1f"), "age" : 22 }
//模糊查询
> db.user.find({"name":/zhangsan/})
//查找以什么开头的模糊查询
>db.user.find({"name":/^zhang/})
//查找以什么结尾的模糊查询
>db.user.find({"name":/zhang$/})
//查询指定name,age数据
>db.user.find({},{name:1})
>db.user.find({},{age:1})
>db.user.find({"age":{$gte:20}},{age:1})
//查询name为wangwu,age为25
>db.user.find({"name":"wangzu","age":25})
//查询前5条数据
>db.user.find().limt(2)
//查询10条以后的数据
>db.user.find().skip(10)
//skip和limt可以链式查询 连接起来查询

排序

//升序
>db.user.find().sort({"age":1})
//降序
>db.user.find().sort({"age":1})

删除数据库

 > db.dropDatabase()
{ "ok" : 1 }

删除某一个表(集合)

> db.tokens.drop()
true

连贯操作

在这里插入图片描述

查询统计数据库数据条数

> use xjts
switched to db xjts
> for(var i=0;i<99;i++){
... db.user.insert({"name":"zhangsan"+i,"age":i})
... };
> db.user.find().count()
99
> db.user.find({age:{$gte:66}}).count()
33
//第一页
> db.user.find().skip(0).limt(10)
//第二页
> db.user.find().skip(10).limt(10)
> //第三页
(3-1)*10
> db.user.find().skip(20).limt(10)
算法:skip的page-1然后乘以pageSzie,然后limt(pageSize)

or 与查询

> db.user.find({$or:[{age:22},{age:25}]})
{ "_id" : ObjectId("6121f464e9b431717ff1f417"), "name" : "zhangsan22", "age" : 22 }
{ "_id" : ObjectId("6121f464e9b431717ff1f41a"), "name" : "zhangsan25", "age" : 25 }

修改数据

> db.user.update({age:25},{$set:{age:66666}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({age:66666})
{ "_id" : ObjectId("6121f464e9b431717ff1f41a"), "name" : "zhangsan25", "age" : 66666 }
//添加修改属性
> db.user.update({age:66666},{$set:{age:655,sex:"男人"}})))
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })                          })
> db.user.find({age:655})
{ "_id" : ObjectId("6121f464e9b431717ff1f41a"), "name" : "zhangsan25", "age" : 655, "sex" : "男人" }
//如果没有$set则会重写覆盖这条数据
> db.user.update({age:655},{age:888})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({age:888})
{ "_id" : ObjectId("6121f464e9b431717ff1f41a"), "age" : 888 }
//更新多条数据 {multi:true}
> db.user.update({age:{$lte:5}},{$set:{"sex":"男"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find().limit(5)                          )
{ "_id" : ObjectId("6121f464e9b431717ff1f401"), "name" : "zhangsan0", "age" : 0, "sex" : "男" }
{ "_id" : ObjectId("6121f464e9b431717ff1f402"), "name" : "zhangsan1", "age" : 1 }
{ "_id" : ObjectId("6121f464e9b431717ff1f403"), "name" : "zhangsan2", "age" : 2 }
{ "_id" : ObjectId("6121f464e9b431717ff1f404"), "name" : "zhangsan3", "age" : 3 }
{ "_id" : ObjectId("6121f464e9b431717ff1f405"), "name" : "zhangsan4", "age" : 4 }
> db.user.update({age:{$lte:5}},{$set:{"sex":"男"}},{multi:true})
WriteResult({ "nMatched" : 6, "nUpserted" : 0, "nModified" : 5 })
> db.user.find().limit(5)                                       )
{ "_id" : ObjectId("6121f464e9b431717ff1f401"), "name" : "zhangsan0", "age" : 0, "sex" : "男" }
{ "_id" : ObjectId("6121f464e9b431717ff1f402"), "name" : "zhangsan1", "age" : 1, "sex" : "男" }
{ "_id" : ObjectId("6121f464e9b431717ff1f403"), "name" : "zhangsan2", "age" : 2, "sex" : "男" }
{ "_id" : ObjectId("6121f464e9b431717ff1f404"), "name" : "zhangsan3", "age" : 3, "sex" : "男" }
{ "_id" : ObjectId("6121f464e9b431717ff1f405"), "name" : "zhangsan4", "age" : 4, "sex" : "男" }
>

删除数据

//可删除一条或多条
>db.user.remove({age:22})
>db.user.remove({age:{$lte:10},{justOne:true}})
WriteResult({ "nRemoved" : 11 })
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Node.js可以通过许多模块和库与数据库进行删改操作。以下是几个常用的模块和示例代码: 1. 使用MySQL数据库示例: ```javascript const mysql = require('mysql'); // 创建数据库连接 const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'password', database: 'mydatabase' }); // 连接数据库 connection.connect((err) => { if (err) { console.error('数据库连接失败: ', err); return; } console.log('成功连接到数据库'); }); // 插入数据 const insertData = (data) => { const query = 'INSERT INTO table_name SET ?'; connection.query(query, data, (err, result) => { if (err) { console.error('数据插入失败: ', err); return; } console.log('成功插入数据'); }); }; // 询数据 const selectData = () => { const query = 'SELECT * FROM table_name'; connection.query(query, (err, result) => { if (err) { console.error('数据询失败: ', err); return; } console.log('询结果: ', result); }); }; // 更新数据 const updateData = (id, newData) => { const query = 'UPDATE table_name SET ? WHERE id = ?'; connection.query(query, [newData, id], (err, result) => { if (err) { console.error('数据更新失败: ', err); return; } console.log('成功更新数据'); }); }; // 删除数据 const deleteData = (id) => { const query = 'DELETE FROM table_name WHERE id = ?'; connection.query(query, id, (err, result) => { if (err) { console.error('数据删除失败: ', err); return; } console.log('成功删除数据'); }); }; // 断开数据库连接 connection.end(); ``` 2. 使用MongoDB数据库示例: ```javascript const mongodb = require('mongodb'); const MongoClient = mongodb.MongoClient; // 连接MongoDB数据库 const url = 'mongodb://localhost:27017'; const dbName = 'mydatabase'; MongoClient.connect(url, (err, client) => { if (err) { console.error('数据库连接失败: ', err); return; } console.log('成功连接到数据库'); const db = client.db(dbName); // 插入数据 const insertData = (data, collectionName) => { const collection = db.collection(collectionName); collection.insertOne(data, (err, result) => { if (err) { console.error('数据插入失败: ', err); return; } console.log('成功插入数据'); }); }; // 询数据 const selectData = (collectionName) => { const collection = db.collection(collectionName); collection.find({}).toArray((err, result) => { if (err) { console.error('数据询失败: ', err); return; } console.log('询结果: ', result); }); }; // 更新数据 const updateData = (id, newData, collectionName) => { const collection = db.collection(collectionName); collection.updateOne({ _id: mongodb.ObjectID(id) }, { $set: newData }, (err, result) => { if (err) { console.error('数据更新失败: ', err); return; } console.log('成功更新数据'); }); }; // 删除数据 const deleteData = (id, collectionName) => { const collection = db.collection(collectionName); collection.deleteOne({ _id: mongodb.ObjectID(id) }, (err, result) => { if (err) { console.error('数据删除失败: ', err); return; } console.log('成功删除数据'); }); }; // 关闭数据库连接 client.close(); }); ``` 这些示例代码演示了如何使用Node.js连接MySQL和MongoDB数据库,并执行常见的删改操作。你可以根据自己的数据库类型和需求进行相应的调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值