習慣沉默2017-04-27 09:04:18
eg:
db.users.insert({name:'tmac',blogid:1});
db.blog.insert({id:1,detail:'tmacs blog'});
users has an attribute blogid and the id of blogs are one-to-one:
db.users.find().forEach(function(x){
var blogs_record = db.blogs.findOne({id:x.blogid});
if(blogs_record != null){
db.temp.insert({name:x.name,detail:blogs_record.detail});
}
)
eg:
db.users.insert({name:'tmac',blogid:1});
db.blog.insert({id:1,detail:'tmacs blog1'});
db.blog.insert({id:1,detail:'tmacs blog2'});
users has an attribute blogid and the id of blogs is a pair of n:
db.users.find().forEach(function(x){
db.blogs.findOne({id:x.blogid}).forEach(function(y){
db.temp.insert({name:x.name,y.detail});
})
)
漂亮男人2017-04-27 09:04:18
Blogs can directly embed the author's information as a sub-document of blogs.
曾经蜡笔没有小新2017-04-27 09:04:18
mongoose provides the populate method to implement join. But I don’t understand it either~
http://www.nodeclass.com/api/mongoose.html#guide_populate
A relatively simple but clumsy method is to check one table first, then forEach the result and then query another table like the answer above.