基于mongoose 的增删改查操作

无论是基于robomongo 的可视化工具,亦或是基于 mongoose 的函数工具,只要是对 mongodb 的操作,第一步都是开启数据库。

开启mongodb 数据库
进入mongod所在目录 执行命令 ./mongod --dbpath=存放数据的位置
例1:./mongod --dbpath d:\MongoDB\db
例2:./mongod --dbpath d:\MongoDB\db --port 自定义端口号,默认27017(了解即可,不推荐使用,修改默认端口号后期维护麻烦)

新增数据

let mongoose = require('mongoose');
let log = console.log.bind(console);
let db = null;
let Schema = mongoose.Schema;

// 链接数据库
mongoose.connect('mongodb://localhost/fay');

db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'))

db.once('open', (cb) => {
	// 设置数据库类型
	let PersonSchema = new Schema({
		name: {
			type: 'String',
			required: true
		},
		age: {
			type: 'Number',
			retuired: true
		}
	});

	// 新建集合(如存在该集合,就选中)
	let PersonModel = db.model('person', PersonSchema);

	// 需要保存的数据
	let data = 	{
		age: 50,
		name: 'leno'
	};

	// 实例化集合并插入数据
	let personEntity = new PersonModel(data);

	//  保存实例
	personEntity.save((err, res) => {
		if(err) return log(err);
		db.close();
	});
})

删除数据

let mongoose = require('mongoose');
let log = console.log.bind(console);
let db = null;
let Schema = mongoose.Schema;

// 链接数据库
mongoose.connect('mongodb://localhost/fay');

db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'))

db.once('open', (cb) => {
	// 设置数据库类型
	let PersonSchema = new Schema({
		name: {
			type: 'String',
			required: true
		},
		age: {
			type: 'Number',
			retuired: true
		}
	});

	// 选择集合(如不存在该集合,就新建)
	let PersonModel = db.model('person', PersonSchema);

	// 删除的条件
	let del = {name: 'leno'};

	// 删除命令
	PersonModel.remove(del, (err, res) => {
		if(err) throw new Error(err);

		log(res);
		db.close();
	})
	
});

修改数据

let mongoose = require('mongoose');
let log = console.log.bind(console);
let db = null;
let Schema = mongoose.Schema;

// 链接数据库
mongoose.connect('mongodb://localhost/fay');

db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'))

db.once('open', (cb) => {
	// 设置数据库类型
	let PersonSchema = new Schema({
		name: {
			type: 'String',
			required: true
		},
		age: {
			type: 'Number',
			retuired: true
		}
	});

	// 选择集合(如不存在该集合,就新建)
	let PersonModel = db.model('person', PersonSchema);

	// 旧数据
	let oldVal = {name: 'leno'};

	// 新数据
	let newVal = {name: 'liao'};
	// 多个新数据
	let newVal2 = {name: 'liao', age: '25'};

	// 修改(更新)命令
	PersonModel.update(oldVal, newVal, (err, res) => {
		if(err) throw new Error(err);

		log(res);
		db.close();
	});
})

查询数据

let mongoose = require('mongoose');
let log = console.log.bind(console);
let db = null;
let Schema = mongoose.Schema;

// 链接数据库
mongoose.connect('mongodb://localhost/fay');

db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'))

db.once('open', (cb) => {
	// 设置数据库类型
	let PersonSchema = new Schema({
		name: {
			type: 'String',
			required: true
		},
		age: {
			type: 'Number',
			retuired: true
		}
	});

	// 选择集合(如不存在该集合,就新建)
	let PersonModel = db.model('person', PersonSchema);

	// 查询条件
	let sql = {name: 'liao'};

	// 查询命令
	PersonModel.find(sql, (err, res) => {
		if(err) throw new Error(err);

		log(res);
		db.close();
	});
});
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碑无名

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值