mongo如何连表查询,比如有一个users 还有一个blogs 输出 blogs内容和作者信息
mongodb不支持关系数据库的join(Connection) Query
mongodb
join
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}); } )
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}); }) )
Blogs can directly embed the author's information as a sub-document of blogs.
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.
mongodb
不支持关系数据库的join
(Connection) Queryeg:
users has an attribute blogid and the id of blogs are one-to-one:
eg:
users has an attribute blogid and the id of blogs is a pair of n:
Blogs can directly embed the author's information as a sub-document of blogs.
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.