Last active
September 27, 2019 05:53
-
-
Save aikin/534fae3c95039b7dee09 to your computer and use it in GitHub Desktop.
mongoose-population
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'); | |
var Schema = mongoose.Schema; | |
var ObjectId = mongoose.Types.ObjectId; | |
console.log('Running mongoose version %s', mongoose.version); | |
var UserSchema = new Schema({ | |
name : String, | |
posts : [{ | |
type : Schema.Types.ObjectId, | |
ref : 'Post' | |
}] | |
}); | |
var User = mongoose.model('User', UserSchema); | |
var PostSchema = new Schema({ | |
poster: { | |
type : Schema.Types.ObjectId, | |
ref : 'User' | |
}, | |
comments: [{ | |
type : Schema.Types.ObjectId, | |
ref : 'Comment' | |
}], | |
title: String | |
}); | |
var Post = mongoose.model('Post', PostSchema); | |
var CommentSchema = new Schema({ | |
post: { | |
type : Schema.Types.ObjectId, | |
ref : "Post" | |
}, | |
commenter: { | |
type : Schema.Types.ObjectId, | |
ref : 'User' | |
}, | |
content: String | |
}); | |
var Comment = mongoose.model('Comment', CommentSchema); | |
mongoose.connect('mongodb://localhost/population-test', function (err) { | |
// if we failed to connect, abort | |
if (err) throw err; | |
// we connected ok | |
createData(); | |
}); | |
function createData() { | |
var userIds = [new ObjectId, new ObjectId, new ObjectId]; | |
var postIds = [new ObjectId, new ObjectId, new ObjectId]; | |
var commentIds = [new ObjectId, new ObjectId, new ObjectId]; | |
var users = []; | |
var posts = []; | |
var comments = []; | |
users.push({ | |
_id : userIds[0], | |
name : 'aikin', | |
posts : [postIds[0]] | |
}); | |
users.push({ | |
_id : userIds[1], | |
name : 'luna', | |
posts : [postIds[1]] | |
}); | |
users.push({ | |
_id : userIds[2], | |
name : 'luajin', | |
posts : [postIds[2]] | |
}); | |
posts.push({ | |
_id : postIds[0], | |
title : 'post-by-aikin', | |
poster : userIds[0], | |
comments : [commentIds[0]] | |
}); | |
posts.push({ | |
_id : postIds[1], | |
title : 'post-by-luna', | |
poster : userIds[1], | |
comments : [commentIds[1]] | |
}); | |
posts.push({ | |
_id : postIds[2], | |
title : 'post-by-luajin', | |
poster : userIds[2], | |
comments : [commentIds[2]] | |
}); | |
comments.push({ | |
_id : commentIds[0], | |
content : 'comment-by-luna', | |
commenter : userIds[1], | |
post : postIds[0] | |
}); | |
comments.push({ | |
_id : commentIds[1], | |
content : 'comment-by-luajin', | |
commenter : userIds[2], | |
post : postIds[1] | |
}); | |
comments.push({ | |
_id : commentIds[2], | |
content : 'comment-by-aikin', | |
commenter : userIds[1], | |
post : postIds[2] | |
}); | |
User.create(users, function(err, docs) { | |
Post.create(posts, function(err, docs) { | |
Comment.create(comments, function(err, docs) { | |
// run example on here | |
}); | |
}); | |
}); | |
} | |
// Query#populate examples | |
function example1(done) { | |
User.find() | |
.populate('posts', 'title', null, {sort: { title: -1 }}) | |
.exec(function(err, docs) { | |
console.log(docs[0].posts[0].title); // post-by-aikin | |
}); | |
User.findOne({name: 'luajin'}) | |
.populate({path: 'posts', select: { title: 1 }, options: {sort: { title: -1 }}}) | |
.exec(function(err, doc) { | |
console.log(doc.posts[0].title); // post-by-luajin | |
}); | |
} | |
function example2(done) { | |
Post.findOne({title: 'post-by-aikin'}) | |
.populate('poster comments', '-_id') | |
.exec(function(err, doc) { | |
console.log(doc.poster.name); // aikin | |
console.log(doc.poster._id); // undefined | |
console.log(doc.comments[0].content); // comment-by-luna | |
console.log(doc.comments[0]._id); // undefined | |
}); | |
Post.findOne({title: 'post-by-aikin'}) | |
.populate({path: 'poster comments', select: '-_id'}) | |
.exec(function(err, doc) { | |
console.log(doc.poster.name); // aikin | |
console.log(doc.poster._id); // undefined | |
console.log(doc.comments[0].content); // comment-by-luna | |
console.log(doc.comments[0]._id); // undefined | |
}); | |
Post.findOne({title: 'post-by-aikin'}) | |
.populate(['poster', 'comments']) | |
.exec(function(err, doc) { | |
console.log(doc.poster.name); // aikin | |
console.log(doc.comments[0].content); // comment-by-luna | |
}); | |
Post.findOne({title: 'post-by-aikin'}) | |
.populate([ | |
{path:'poster', select: '-_id'}, | |
{path:'comments', select: '-content'} | |
]) | |
.exec(function(err, doc) { | |
console.log(doc.poster.name); // aikin | |
console.log(doc.poster._id); // undefined | |
console.log(doc.comments[0]._id); // 会打印出对应的 comment id | |
console.log(doc.comments[0].content); // undefined | |
}); | |
} | |
// Model#populate example | |
function example3(done) { | |
Post.find({title: 'post-by-aikin'}) | |
.populate('poster comments') | |
.exec(function(err, docs) { | |
var opts = [{ | |
path : 'comments.commenter', | |
select : 'name', | |
model : 'User' | |
}]; | |
Post.populate(docs, opts, function(err, populatedDocs) { | |
console.log(populatedDocs[0].poster.name); // aikin | |
console.log(populatedDocs[0].comments[0].commenter.name); // luna | |
done(); | |
}); | |
}); | |
} | |
// Document#populate example | |
function example4(done) { | |
User.findOne({name: 'aikin'}) | |
.exec(function(err, doc) { | |
var opts = [{ | |
path : 'posts', | |
select : 'title' | |
}]; | |
doc.populate(opts, function(err, populatedDoc) { | |
console.log(populatedDoc.posts[0].title); // post-by-aikin | |
done(); | |
}); | |
}); | |
} | |
function done(){ | |
mongoose.connection.db.dropDatabase(function() { | |
mongoose.connection.close(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
非常感谢博主提供的实例代码。
发现一个问题,模拟填充数据时,用户名与ID不匹配。