Mongoose的用法笔记

1.关于Mongoose

1.Mongoose支持的数据类型
    String        --   字符串
    Number        --   数字,包括整数和小数
    Date          --   日期
    Boolean       --   布尔
    Buffer        --   用于存储二进制数据,eg:图片,最大不超过16M
    ObjectId      --   mongodb自动生成_id,作为数据库的主键
    Mixed         --   可以存储任意的数据类型
    Array         --   数组或内置子文档(subdocuments)

说明:
    Array类型的两种用法:
       1.array数组数据
           eg:
              var UserSchema = new mongoose.Schema({
              name:String ,
              email:[String]
              });

       2.内置subdocuments
           eg:
              var EmailSchema = new mongoose.Schema({
              email:String,
              regTime: Date
              });

              var UserSchema = new mongoose.Schema({
              name:String,
              email:[emailSchema]
              });

       3.Mongoose的用法
           eg:
             1.定义Schema结构

              var UserSchema = new mongoose.Schema({
              name:{type:String ,unique:true, index:true} ,
              email:{type:String , unique:true} ,
              regTime:{type:Date , default:Date.now } ,
              modifiedTime: Date,
              lastLogin: Date
              });


             2.Model --- 与MongoDB的document一一对应
                      schema打包成model,model会自动生成所有的  增(save)、删(delete)、改(update)、查(find,findOne,findById)  方法
               生成一个Model
                    mongoose.model('User',UserSchema)

               eg:
                  查询年龄在21~65岁之间的User
                          var User = require('./user');

                          User.find({age:{$gte:21, $lte:65}},callback);
                      <=等价于=>
                          User.where('age').gte(21).lte(65).exec(callback);


2.Mongoose进行CRUD操作

Model methods and instance methods

So what do we mean by model methods? Well, if we have a model called User, some of the methods provided by Mongoose are User.create, User.find, User.update, and User.remove. The method names may be slightly different, but out of the box we’ve got methods for all four CRUD (Create, Read, Update, and Delete) operations right there.

Creation
– Adding data to the instance

 var newUser = new User({
     name: 'Simon Holmes',
     email: 'simon@theholmesoffice.com',
     lastLogin : Date.now()
});

– Saving an instance

newUser.save( function( err ){
     if(!err){
       console.log('User saved!');
     }
});

– Using the saved data

newUser.save( function( err, user ){
     if(!err){
       console.log('Saved user name: ' + user.name);
       console.log('_id of saved user: ' + user._id);
     }
});

Querying
There are a number of static model methods provided by Mongoose to assist in the bulk of find operations, including
– Model.find
– Model.findOne
– Model.findById

 User.find({'name' : 'Simon Holmes'})
   .where('age').gt(18)
   .sort('-lastLogin')
   .select('_id name email')
   .exec(function (err, users){
     if (!err){
       console.log(users); // output array of users found
} });

Model.findOne

 User.findOne({'name' : 'Simon Holmes'})
   .where('age').gt(18)
   .sort('-lastLogin')
   .select('_id name email')
   .exec(function (err, users){
     if (!err){
       console.log(users); // output array of users found
} });

Model.fin

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值