Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as JavaScript by wit ( 8 years ago )
var mongoose = require('mongoose');
mongoose.connect("mongodb://localhost/asso_ref");
var postSchema = new mongoose.Schema({
title:String,
content:String
});
var Post = mongoose.model("Post",postSchema);
var userSchema = new mongoose.Schema ({
email:String,
name:String,
posts:[{
type:mongoose.Schema.Types.ObjectId,
ref:"Post" //match with .model>> ("POST") <<
}]
});
var User = mongoose.model("user",userSchema);
User.create({
email:"[email protected]",
name:"Sirawit Mahanin"
});
Post.create({title:"Post 1", content:"This is post 1"},
function(err,post) {
if(!err){
User.findOne({email:"[email protected]"},function(err,foundUser) {
if(!err) {
foundUser.posts.push(post);
foundUser.save(function(err,post) {
if(!err){
console.log(post);
}
});
}
});
}
}
);
// FIND USER
User.find({email:"[email protected]"}).populate("posts").exec(function(err,user){
if(err){
console.log(err);
} else {
console.log(user);
}
});
Revise this Paste