node.js Mongoose/mongoDB 查询加入.. 但我来自 sql 背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14363065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:58:04  来源:igfitidea点击:

Mongoose/mongoDB query joins.. but I come from a sql background

node.jsmongodbmongoose

提问by nwkeeley

I coming from a sql background so writing queries in sql where I join tables is quite simple but I guess I am missing that in mongoose/mongodb

我来自 sql 背景,所以在我加入表的 sql 中编写查询非常简单,但我想我在 mongoose/mongodb 中缺少它

Basically I know the Subscriber_ID (which maps to a document in the User Collection)

基本上我知道 Subscriber_ID (它映射到用户集合中的文档)

I want to pull the project group, with all the projects that the user belongs to so if I was to write this in pseduo sql it would be like

我想拉出项目组,以及用户所属的所有项目,所以如果我在 pseduo sql 中编写它,它会像

Select 
  ProjectGroup.title, 
  Project.Title 
FROM 
  ProjectGroup, 
  Project, 
  User 
WHERE 
  User.id = req.body.subscriber_id 
  AND Project.subscriber_id = User.id 
  AND  ProjectGroup.project_id = Project.id

There must be a way to do similiar joins in mongoose/mongodb because the type is mapping to a schema right?

必须有一种方法可以在 mongoose/mongodb 中进行类似的连接,因为类型映射到模式对吗?

My Schemas.....

我的架构.....

Project Group Schema

项目组架构

var ProjectGroupSchema = new Schema({
    title             : String
  , projects          : [ { type: Schema.Types.ObjectId, ref: 'Project' } ]
});

Project Schema

项目架构

var ProjectSchema = new Schema({
    title         : {type : String, default : '', required : true}
  , subscribers   : [{ type: Schema.Types.ObjectId, ref: 'User' }]
});

User Schema

用户架构

var UserSchema = new Schema({
    first_name    : {type: String, required: true}
  , last_name     : {type: String, required: true}
});

Thank you!

谢谢!

回答by Michael Yin

You are just one step away!

你只差一步之遥!

Project Group Schema:

项目组架构:

var ProjectGroupSchema = new Schema({
    title             : String
});

Project Schema:

项目架构:

var ProjectSchema = new Schema({
    title         : {type : String, default : '', required : true},
    group         : {type: Schema.Types.ObjectId, ref: 'ProjectGroup' },
    _users    : [{type: Schema.Types.ObjectId, ref: 'User' }]
});

User Schema:

用户架构:

var UserSchema = new Schema({
    first_name    : {type: String, required: true},
    last_name     : {type: String, required: true},
    subscribing   : [{type: Schema.Types.ObjectId, ref: 'Project' }]
});

Then you can do the following:

然后您可以执行以下操作:

user.findById(req.userId)
     .populate('subscribing')
     .exec(function(err, user){
          console.log(user.subscribing);
     })

Or:

或者:

project.find({
        subscriber : req.userId
      })
     .populate('subscriber')
     .populate('group')
     .exec(function(err, projects){
          console.log(projects);
     })

回答by joscas

There are no joins in Mongodb. This question I think is a good reference:

Mongodb 中没有连接。这个问题我认为是一个很好的参考:

MongoDB and "joins"

MongoDB 和“加入”

To summarize, different strategies have to be adopted with mongodb for problems that would be addressed via joins in relational DBs. I would say you mainly end-up doing one of these two things:

总而言之,对于通过关系数据库中的连接解决的问题,mongodb 必须采用不同的策略。我会说你主要是做以下两件事之一:

  • Embedding: You embed information in a single document that would in a relational DB be distributed amongst different tables.
  • Joining information client-side: When you need to use information from several places, you query many times and then put the pieces together in your client.
  • 嵌入:您将信息嵌入到单个文档中,该文档将在关系数据库中分布在不同的表中。
  • 加入信息客户端:当您需要使用来自多个地方的信息时,您可以多次查询,然后在您的客户端中将这些碎片放在一起。