Created
May 18, 2011 20:06
-
-
Save KATT/979422 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Adding relationship. Pretty neat. | |
Let's say I have a User in var user1, user2: | |
**/ | |
var friends = new UserFriends(); | |
friends.users = [user2._id,user1._id]; | |
friends.save(); | |
/** | |
Fetching friends. Not as neat. | |
Let's say I have a User object in the var `usr`, now I want to fetch all their friends | |
**/ | |
// I came up with this function | |
function findFriendsOf(myUser, _callback) { | |
console.log("Looking for friends of", myUser.user, myUser._id); | |
UserFriends.find({users: myUser._id},function(err, friends) { | |
var friendsIds = []; | |
// Let's look through our friends and gather an array with our friends' user ids | |
for (var i in friends) { | |
var user1 = friends[i].users[0]; | |
var user2 = friends[i].users[1]; | |
friendsIds.push(myUser._id != user1 | |
? user1 | |
: user2 | |
); | |
} | |
User.find({_id: {$in: friendsIds}}, function(err, users) { | |
// Finally, here should we have all our friends in var users | |
_callback(err, users); | |
}) | |
}); | |
} | |
findFriendsOf(usr, function(err,users) { | |
console.log("users:",users); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'), | |
Schema = mongoose.Schema, | |
User = require('./UserSchema'); | |
var FriendsSchema = new Schema({ | |
users : [User.ObjectId], | |
confirmed : { type: Boolean, default: false, index: true }, | |
created : {type: Date, default: Date.now}, | |
}); | |
FriendsSchema.index({users: 1}, {unique:true}); // Fine, ensures we only have one of these relations | |
module.exports = FriendsSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var UserSchema = new Schema({ | |
user : { type: String, unique: true, validate: /^[A-Z0-9._%+-]+$/i }, | |
password : { type: String, index: true, set: setPassword }, | |
email : { type: String, unique: false, set: toLower, validate: /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i }, | |
confirmed : { type: Boolean, default: false, index: true }, | |
apiKey : { type: String, unique: true }, | |
created : {type: Date, default: Date.now}, | |
modified : {type: Date, default: Date.now}, | |
name: { | |
first: String, | |
last: String | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment