标签:mod 插入数据 cal else log 文档 多对多 tle pre
**********************
作者: 张启卫
时间: 2017年5月23号
功能: mongoDB 与 mongoose 数据关系模型
* 一对一
* 一对多
* 多对多
**********************
var mongoose = require(‘mongoose‘); mongoose.connect("mongodb://localhost/data_test");
var userSchema = new mongoose.Schema({
email: String,
name: String,
});
var User = mongoose.model("User", userSchema);
var newUser = new User({ email: "774026263@qq.com", name: "启卫" });
newUser.save(function(err, user){ if(err) { console.log(err); } else { console.log(user); } });
node embed.js
// Post - title, content var postSchema = new mongoose.Schema({ title: String, content: String });
var Post = mongoose.model(‘Post‘, postSchema);
var newPost = new Post({ title: "MongoDB数据模型", content: "Embeded && Referencing" });
newPost.save(function(err, post){ if(err) { console.log(err); } else { console.log(post); } });
node embed.js
// Post - title, content var postSchema = new mongoose.Schema({ title: String, content: String }); var Post = mongoose.model(‘Post‘, postSchema); // User - email, name var userSchema = new mongoose.Schema({ email: String, name: String, posts: [postSchema] }); var User = mongoose.model("User", userSchema);
注意: 这里必须将postSchma定义在userSchema之前,否则userSchema在验证时会报 postSchema 未定义
var newUser = new User({ email: "774026263@qq.com", name: "启卫" });
newUser.posts.push({ title: "无法上网", content: "电脑重启过后,无法上网" });
newUser.save(function(err, user){ if(err) { console.log(err); } else { console.log(user); } });
$ node embed.js
{ _id: 5923f3d77ada40113ada1dcf,
email: ‘774026263@qq.com‘,
name: ‘启卫‘,
__v: 1,
posts:
[ { title: ‘无法上网‘,
content: ‘电脑重启过后,无法上网‘,
_id: 5923f3d77ada40113ada1dd0 },
{ title: ‘3 things I really hate‘,
content: ‘codeing, coding, coding‘,
_id: 5923f6d7d4f39e1258d6ebcb } ] }
^C
User.findOne({name: "启卫"}, function(err, user) { if(err) { // console.log(err); } else { console.log(user); } });
David@zhangqiweideMacBook-Pro:~/04_web-fullstack/07_Express/DataAssociations$ node embed.js { _id: 5923f3d77ada40113ada1dcf, email: ‘774026263@qq.com‘, name: ‘启卫‘, __v: 0, posts: [ { title: ‘无法上网‘, content: ‘电脑重启过后,无法上网‘, _id: 5923f3d77ada40113ada1dd0 } ] } ^C
User.findOne({name: "启卫"}, function(err, user) { if(err) { // console.log(err); } else { // console.log(user); //一旦查找到人,则插入post user.posts.push({ title: "3 things I really hate", content: "codeing, coding, coding" }); user.save(function(err, user) { if(err) { console.log(err); } else { console.log(user); } }); } });
$ node embed.js { _id: 5923f3d77ada40113ada1dcf, email: ‘774026263@qq.com‘, name: ‘启卫‘, __v: 1, posts: [ { title: ‘无法上网‘, content: ‘电脑重启过后,无法上网‘, _id: 5923f3d77ada40113ada1dd0 }, { title: ‘3 things I really hate‘, content: ‘codeing, coding, coding‘, _id: 5923f6d7d4f39e1258d6ebcb } ] } ^C
var mongoose = require(‘mongoose‘); mongoose.connect("mongodb://localhost/references"); // Post - title, content var postSchema = new mongoose.Schema({ title: String, content: String }); var Post = mongoose.model(‘Post‘, postSchema); // User - email, name var userSchema = new mongoose.Schema({ email: String, name: String, posts: [ { type: mongoose.Schema.Types.ObjectId, ref: "Post" } ] }); var User = mongoose.model("User", userSchema);
注意:在userSchema的posts中, 将type变为ObjectId, 将ref引用写成 Post
User.create({ email: "mzhangqiwei@hotmail.com", name: "David" });
Post.create({ title: "电脑现在已经可以上网了", content: "网络现在已经通畅了" }, function(err, post) { if(err) { console.log(err); } else { // console.log(post); User.findOne({email: "mzhangqiwei@hotmail.com"}, function(err, foundUser) { if(err) { console.log(err); } else { foundUser.posts.push(post); foundUser.save(function(err, data) { if(err) { console.log(err); } else { console.log(data); } }); } }); } });
注意: 首先测试Post.create能否正常将数据插入数据库,然后查找email为mzhangqiwei@hotmail.com的用户,将post数据插入,将保存数据
//查找用户 //查找那个用户的所有post User.findOne({email: "mzhangqiwei@hotmail.com"}).populate(‘posts‘).exec(function(err, user){ if(err) { console.log(err); } else { console.log(user); } });
标签:mod 插入数据 cal else log 文档 多对多 tle pre
原文地址:http://www.cnblogs.com/elewei/p/6895316.html