简体   繁体   English

在Node Js中连接来自mongoDB的两个不同集合

[英]Joining two different collections form mongoDB in Node Js

I am new to mongoDB, and was wondering if the following is possible. 我是mongoDB的新手,并想知道以下情况是否可能。

I have two different collection in the same mongo db table - called jobs and nodes and this is that they look like: 我在同一个mongo db表中有两个不同的集合-称为jobsnodes ,这看起来像:

function testing() {
    nodes.find(function(err, data) {
        if (err) {
            console.log(err)
        } else {
            console.log('NODES RETURNED: ', data)

            jobs.find(function(err, post) {
                if (err) {
                    console.log(err)
                } else {
                    console.log('JOBS RETURNED: ', post)
                }
            });
        }
    });
}

Which returns the following: 返回以下内容:

JOBS RETURNED:  [ { _id: '5899999354d59',
    job_url: 'http://222.22.22.22:2222/jobs',
    progress: 0,
    queue: 0 },
  { _id: '5899b7d054da96',
    job_url: 'http://111.11.1.111:1111/jobs',
    progress: 0,
    queue: 0 } ]

CLUSTER NODESS RETURNED:  [ { _id: '58a9a4805c1f',
    node_url: 'http://222.22.22.22:2222/nodes',
    cpu: 40 },
  { _id: '58999a9a4805c23',
    node_url: 'http://111.11.1.111:1111/nodes',
    average_cpu: 15 } ]

So as you can see, the two different collections both have two documents each, and they can relate by the job_url and the node_url eg 222.22.22.22:2222 Is it possible for me to join the documents together based on this, so that the final result is something like this: 如您所见,两个不同的集合每个都有两个文档,它们可以由job_url和node_url关联,例如222.22.22.22:2222我是否可以基于此将文档合并在一起,以便最终结果是这样的:

[ { _id: '58a9a4805c1f',
    node_url: 'http://222.22.22.22:2222/nodes',
    job_url: 'http://222.22.22.22:2222/jobs',
    progress: 0,
    queue: 0 },
    cpu: 40 },
  { _id: '58999a9a4805c23',
    node_url: 'http://111.11.1.111:1111/nodes',
    job_url: 'http://111.11.1.111:1111/jobs',
    progress: 0,
    queue: 0,
    average_cpu: 15 } 

Any help / tips would be really appreciated! 任何帮助/提示将不胜感激!

There is no join in mongo because mongo is a nosql database, read about it here https://en.wikipedia.org/wiki/NoSQL mongo中没有联接,因为mongo是一个nosql数据库,请在此处阅读有关它的信息https://en.wikipedia.org/wiki/NoSQL

In brief, nosql databases are used when fast retrieval and high availability of data is needed (join is slow so it's out of the game here), instead of that, when you get into situation like yours then you should think of remodeling your schema as explained well here https://docs.mongodb.com/manual/core/data-modeling-introduction 简而言之,当需要快速检索和数据的高可用性(join速度很慢,因此不在这里)时,将使用nosql数据库,而不是那样,当您遇到类似情况时,您应该考虑将模式重构为在这里很好地解释了https://docs.mongodb.com/manual/core/data-modeling-introduction

Ofcource you can do the join manually by yourself but then you miss the point of mongo 当然,您可以自己手动进行加入,但是您错过了mongo的要点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM