Mongoose--Model(增删查改)

本文介绍了如何使用Mongoose进行MongoDB操作,包括模型创建、文档增删查改等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建Model

var schema = new mongoose.Schema({ name: 'string', size: 'string' });
var Tank = mongoose.model('Tank', schema);

Adding

var Tank = mongoose.model('Tank', yourSchema);

var small = new Tank({ size: 'small' });
small.save(function (err) {
  if (err) return handleError(err);
  // saved!
})

// or

Tank.create({ size: 'small' }, function (err, small) {
  if (err) return handleError(err);
  // saved!
})

// or

small.save(fn?)

Querying

Finding documents is easy with Mongoose, which supports the rich query syntax of MongoDB. Documents can be retreived using each models findfindByIdfindOne, or where static methods.

Tank.find({ size: 'small' }).where('createdDate').gt(oneYearAgo).exec(callback);

Removing

Models have a static remove method available for removing all documents matching conditions.

Tank.remove({ size: 'large' }, function (err) {
  if (err) return handleError(err);
  // removed!
});

Updating

Each model has its own update method for modifying documents in the database without returning them to your application. See the API docs for more detail.

 

转载于:https://my.oschina.net/u/3412211/blog/1617075

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值