Mongodb的populate的用法

Mongodb的populate的用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
name : { type: String, unique: true },
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,
content : 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);

  • 在上述的例子中,创建了三个 Models:User,Post,Comment。
  • User 的属性 posts,对应是一个 ObjectId 的数组。ref表示关联Post(注意: 被关联的model的 type 必须是ObjectId, Number, String, 和 Buffer 才有效)。
  • Post的属性 poster 和 comments 分别关联User和Comment。
  • Comment的属性 post 和 commenter 分别关联Post和User。
  • 三个 Models 的关系:一个 user–has many–>post。一个 post–has one–>user,has many–>comment。一个 comment–has one–>post 和 user。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 连接数据库
mongoose.connect('mongodb://localhost/population-test', function (err){
if (err) throw err;
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) {
});
});
});
}

Query#populate的语法

语法:
Query.populate(path, [select], [model], [match], [options])

参数:
path
  类型:String或Object。
  String类型的时, 指定要填充的关联字段,要填充多个关联字段可以以空格分隔。
  Object类型的时,就是把 populate 的参数封装到一个对象里。当然也可以是个数组。下面的例子中将会实现。

select
  类型:Object或String,可选,指定填充 document 中的哪些字段。
  Object类型的时,格式如:{name: 1, _id: 0},为0表示不填充,为1时表示填充。
  String类型的时,格式如:”name -_id”,用空格分隔字段,在字段名前加上-表示不填充。详细语法介绍query-select

model
  类型:Model,可选,指定关联字段的 model,如果没有指定就会使用Schema的ref。

match
  类型:Object,可选,指定附加的查询条件。

options
  类型:Object,可选,指定附加的其他查询选项,如排序以及条数限制等等。

1
2
3
4
5
6
7
8
9
10
11
12
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
});
});