zoukankan      html  css  js  c++  java
  • Mongoose 表实例

      1 /**********     用户表                BY  Jaysir         2015.6.21
      2 ***********
      3 ***********                    可搜索以下关键词来查看未实现功能    
      4 ***********    
      5 ***********                    TODO    :     未完成
      6 ***********                    DONE    :     已完成
      7 ***********                    TOTEST  :     待测试
      8 ***********                    NOTEST  :     无需测试
      9 ***********                    WAITING :     功能待定
     10 ***********
     11 ***********            接口:    (暂未实现 增加分组,更改好友分组等功能)
     12 ***********                            findOneByEmail     (email ,callback)    ;
     13 ***********                            addFriend         (myEmail,otherEmail,callback);
     14 ***********                            delFriend         (myEmail,otherEmail,callback);
     15 ***********                            getFriendList    (email , callback);
     16 ***********                            regInitData        (options , callback);
     17 ***********                            updateInf        (options , callback);
     18 ***********/
     19 var mongoose = require('mongoose');
     20 var Schema = mongoose.Schema;
     21 var userModel = mongoose.model('users', UserSchema);        //
     22 // 定义用户表结构
     23 var UserSchema = new Schema({
     24     user_email        :    {type : String , required : true},
     25     user_password    :    {type : String , required : true},
     26     user_nickname    :    {type : String , required : true},
     27     user_sex        :    {type : String , required : true,enum:['男','女']},
     28     user_pic        :    {type : String , required : true},
     29     
     30     user_friend_groups:[
     31             {
     32                 g_name    : String,
     33                 g_sort    : Number,
     34                 list    : [
     35                     {
     36                         friend : {type:Schema.Types.ObjectId,ref:'users'},        //    引用users表   查询时可以解引用即为用户的好友列表
     37                         remark : {type:String}
     38                     }
     39                 ]
     40             }
     41         ],
     42     user_reg_time    :    {type : Date , required : false}
     43 });
     44 
     45 //        DONE       NOTEST
     46 UserSchema.statics.findOneByEmail = function(email, callback) {
     47     this.findOne({user_email:email}, callback);
     48 };
     49 
     50 
     51 //添加好友    DONE,        TOTEST        //查询代码(以下几行注释为命令行下测试代码)mongo命令行下已测试
     52 // db.users.update({user_email:"jaysir@163.com","user_friend_groups.g_sort":1},    //user_email,g_sort需存在
     53 //                   {"$push": {
     54 //                               "user_friend_groups.$.list":{ 
     55 //                                                           friend  :  {
     56 //                                                                       "$ref" : "users",
     57 //                                                                       "$id"  : ObjectId("55867dd74389afa216e60fad")
     58 //                                                           }, 
     59 //                                                           remark  :  "呵呵1" 
     60 //                               }
     61 //                   }});
     62 UserSchema.statics.addFriend = function(myEmail,otherEmail,callback){
     63     var myId , otherId;
     64     var self = this;
     65     self.findOneByEmail(myEmail,function(err,doc){
     66         myId = doc._id;
     67         self.findOneByEmail(otherEmail,function(err,doc){
     68             otherId = doc._id;
     69             if(!otherId){callback("不存在的用户!");return;}
     70             //添加好友都默认添加到  “我的好友”  列表  
     71             self.update({_id:myId},{"$push":{"user_friend_groups.0.list":{friend:otherId,remark:""}}},callback(err));
     72             self.update({_id:otherId},{"$push":{"user_friend_groups.0.list":{friend:myId,remark:""}}},callback(err));
     73         });
     74     });
     75     // // 介于js异步执行,这里将添加好友放至 取得myId 与 otherId 后的回调里嵌套执行。防止未取得Id值就执行添加好友到列表而错误
     76 }
     77 //删除好友  DONE,        TOTEST        //查询代码(以下几行注释为命令行下测试代码)mongo命令行下已测试(同addFriend代码一起测试)
     78 // db.users.update({    user_email:"jaysir@163.com",
     79 //                      "user_friend_groups.list.remark":"呵呵1"
     80 //                 },
     81 //                 {"$pull":{
     82 //                             "user_friend_groups.$.list":{remark:"呵呵1"}
     83 //                         }
     84 //                 })
     85 UserSchema.statics.delFriend = function(myEmail,otherEmail,callback){
     86     var myId , otherId;
     87     var myId , otherId;
     88     var self = this;
     89     self.findOneByEmail(myEmail,function(err,doc){
     90         myId = doc._id;
     91         self.findOneByEmail(otherEmail,function(err,doc){
     92             otherId = doc._id;
     93             if(!otherId){callback("不存在的用户!");return;}
     94             self.update({_id:myId,"user_friend_groups.list.friend":otherId},{"$pull":{"user_friend_groups.$.list":{friend:otherId}}},callback(err));
     95             self.update({_id:otherId,"user_friend_groups.list.friend":myId},{"$pull":{"user_friend_groups.$.list":{friend:myId}}},callback(err));
     96         });
     97     });
     98 }
     99 //用户登陆后 获取初始化的数据        DONE  TOTEST        
    100 //此处获取用户好友列表并解引用可以直接  result.user_friend_groups[0].list[0].friend.user_email,访问用户好友的Email等信息
    101 //用户信息安全考虑,屏蔽掉解引用后好友的部分信息(未屏蔽,实现方案待定 WAITING)
    102 UserSchema.statics.getFriendList = function(email , callback){
    103     var self = this;
    104     self.findOne({user_email:email}).populate("user_friend_groups.list.friend").exec().then(function(result){
    105         //对用户好友列表进行排序 
    106         result.user_friend_groups.sort(function(obj1,obj2){return obj1.g_sort-obj2.g_sort;});
    107         callback(result);
    108     });
    109 }
    110 //用户注册 初始化数据        DONE  TOTEST
    111 
    112 UserSchema.statics.regInitData = function(options , callback){
    113     var userInf = {
    114         user_email        :     options.email ? options.email : options.user_email,
    115         user_password    :     options.password ? options.password : options.user_password,
    116         user_nickname     :     (options.nickname||options.user_nickname) ? (options.nickname||options.user_nickname) : "",
    117         user_sex        :     (options.sex == "女")||(options.user_sex == "女") ? "女" : "男",
    118         user_pic         :     "url",
    119         "user_friend_groups.0" : {
    120                     g_name : "我的好友",
    121                     g_sort : 0,
    122                     list   : []
    123                 },
    124         user_reg_time     :     Date()
    125     }
    126     var newUser = new userModel(userInf);
    127     newUser.save(callback());
    128 }
    129 
    130 //更新用户信息
    131 UserSchema.statics.updateInf = function(options,callback){
    132     //TODO
    133 }
    134 
    135 module.exports = mongoose.model('users', UserSchema);

    原创,转载请著名

  • 相关阅读:
    NYOJ题目28大数阶乘
    网页小图标设置
    Sass中文乱码问题(手动编译和watch编译)
    设计模式之构建者模式(Builder):初步理解
    Struts2之类型转换器
    css设置网页文本选中样式
    由超市临时储物柜引发的一点设计随想...
    前端资源相关参考资料
    Struts2拦截器之ExceptionMappingInterceptor(异常映射拦截器)
    Struts2之OGNL
  • 原文地址:https://www.cnblogs.com/jaysir/p/4592844.html
Copyright © 2011-2022 走看看