Mongoose Table Instances

Source: Internet
Author: User
Tags findone

1 /********** User table by Jaysir 2015.6.212 ***********3 *********** can search the following keywords to view the features that are not implemented4 ***********    5 *********** TODO: Not completed6 *********** Done: Completed7 *********** totest: Pending Test8 *********** notest: no test required9 *********** Waiting: function to be determinedTen *********** One *********** Interface: (not implemented to add groups, change the group of Friends and other functions) A *********** findonebyemail (email, callback); - *********** Addfriend (myemail,otheremail,callback); - *********** Delfriend (myemail,otheremail,callback); the *********** getfriendlist (email, callback); - *********** reginitdata (options, callback); - *********** Updateinf (options, callback); - ***********/ + varMongoose = require (' Mongoose ')); - varSchema =Mongoose. Schema; + varUsermodel = Mongoose.model (' users ', Userschema);// A //Defining the user table structure at varUserschema =NewSchema ({ -User_email: {type:string, Required:true}, -User_password: {type:string, Required:true}, -User_nickname: {type:string, Required:true}, -User_sex: {type:string, Required:true, enum:[' male ', ' female ']}, -User_pic: {type:string, Required:true}, in      - user_friend_groups:[ to             { + g_name:string, - G_sort:number, the list: [ *                     { $Friend: {type:schema.types.objectid,ref: ' Users '},//A friend list that you can dereference as a user when you reference the Users table queryPanax Notoginseng remark: {type:string} -                     } the                 ] +             } A         ], theUser_reg_time: {type:date, Required:false} + }); -  $ //Done Notest $UserSchema.statics.findOneByEmail =function(email, callback) { -      This. FindOne ({User_email:email}, callback); - }; the  - Wuyi //add friend done, Totest//query code (the following lines comment to test code below the command line) MONGO the command line has been tested the //db.users.update ({user_email: "[email protected]", "User_friend_groups.g_sort": 1},//user_email,g_sort required to exist  - //{"$push": { Wu //"User_friend_groups.$.list": { - //friend: { About //" $ref": "Users", $ //"$id": ObjectId ("55867dd74389afa216e60fad") /c11> - //                                                           },  - //remark: "Hehe 1" - //                               } A //                   }}); +UserSchema.statics.addFriend =function(myemail,otheremail,callback) { the     varmyId, Otherid; -     varSelf = This; $Self.findonebyemail (Myemail,function(err,doc) { theMyId =doc._id; theSelf.findonebyemail (Otheremail,function(err,doc) { theOtherid =doc._id; the             if(!otherid) {Callback ("non-existent user! ");return;} -             //add friends are added by default to my friends list inSelf.update ({_id:myid},{"$push": {"user_friend_groups.0.list": {friend:otherid,remark: ""}}},callback (Err)); theSelf.update ({_id:otherid},{"$push": {"user_friend_groups.0.list": {friend:myid,remark: ""}}},callback (Err)); the         }); About     }); the     ////Between JS asynchronous execution, here will add friends put to get myID and Otherid after the callback nested execution. Prevents an ID value from being taken to add friends to the list without error the } the //delete Buddy done, Totest//query code (following lines comment for test code under command line) MONGO test under command line (test with addfriend code) + //db.users.update ({user_email: "[email protected]", - //" User_friend_groups.list.remark": "Hehe 1" the //                 },Bayi //{"$pull": { the //"User_friend_groups.$.list": {remark: "Hehe 1"} the //                         } - //                 }) -UserSchema.statics.delFriend =function(myemail,otheremail,callback) { the     varmyId, Otherid; the     varmyId, Otherid; the     varSelf = This; theSelf.findonebyemail (Myemail,function(err,doc) { -MyId =doc._id; theSelf.findonebyemail (Otheremail,function(err,doc) { theOtherid =doc._id; the             if(!otherid) {Callback ("non-existent user! ");return;}94Self.update ({_id:myid, "user_friend_groups.list.friend": otherid},{"$pull": {"User_friend_groups.$.list": {friend:otherid}}},callback (err)); theSelf.update ({_id:otherid, "user_friend_groups.list.friend": myid},{"$pull": {"User_friend_groups.$.list": {friend:myid}}},callback (err)); the         }); the     });98 } About //get initialized data after user login done Totest - //here to get the user friend list and dereference can be directly result.user_friend_groups[0].list[0].friend.user_email, access to the user's friend's email and other information101 //User Information security considerations, shielding off the reference after the friend's part of the information (unshielded, the implementation of the plan to be waiting)102UserSchema.statics.getFriendList =function(email, callback) {103     varSelf = This;104Self.findone ({user_email:email}). Populate ("User_friend_groups.list.friend"). EXEC (). Then (function(Result) { the         //sort the list of user friends106Result.user_friend_groups.sort (function(OBJ1,OBJ2) {returnobj1.g_sort-Obj2.g_sort;});107 callback (result);108     });109 } the //user Registration initialization data done Totest111  theUserSchema.statics.regInitData =function(options, callback) {113     varUserinf = { theUser_email:options.email?Options.email:options.user_email, theUser_password:options.password?Options.password:options.user_password, theUser_nickname: (options.nickname| | Options.user_nickname)? (options.nickname| | Options.user_nickname): "",117User_sex: (Options.sex = = "female") | | (Options.user_sex = = "female")? "Female": "Male",118User_pic: "url",119"User_friend_groups.0" : { -G_name: "My Friends",121g_sort:0,122 list: []123                 },124 user_reg_time:date () the     }126     varNewUser =NewUsermodel (userinf);127 Newuser.save (callback ()); - }129  the //Update user Information131USERSCHEMA.STATICS.UPDATEINF =function(options,callback) { the     //TODO133 }134 135Module.exports = Mongoose.model (' users ', Userschema);

Original, reproduced please famous

Mongoose Table Instances

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.