如何在父母关系中以猫鼬建立一对多关系? [英] How to populate one to many relationship in mongoose with parent refrencing?

查看:54
本文介绍了如何在父母关系中以猫鼬建立一对多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个集合:parent,child1,child2.

父集合:

  [{_ id:1},{_ id:2},{_ id:3}] 

child1集合:

  [{{_ id:1,child1:'a',parent:1},{_id:2,child1:'b',parent:1},{_id:3,child1:'c',父母:2}] 

child2集合:

  [{_ id:1,child1:'d',parent:2},{_id:2,child1:'e',parent:3}] 

引用父集合的集合child1和child2.

现在,我想用猫鼬查询以得到如下结果:

  [{_id:1,child1:[{__ id:1,child1:'a'},{_id:2,child1:'b'}],child2:[],},{_id:2,child1:[{__ id:2,child1:'c'}],child2:[{{_ id:1,child1:'d'}]​​,},{_id:3,child1:[],child2:[{__ id:2,child1:'e'}],},] 

我在Aggregate中的尝试正常工作:

  db.parent.aggregate([{"$ lookup":{来自":"child1","localField":"_ id","foreignField":父代","as":"child1"}},{"$ lookup":{来自":"child2","localField":"_ id","foreignField":父代","as":"child2"}}]) 

蒙戈游乐场

当集合位于同一数据库中,而我的集合位于单独的数据库中时,此方法有效.

例如:父数据库中的父集合以及子数据库中的child1和child2集合.

您对跨多个数据库使用聚合有任何想法吗,或者如何使用填充来解决此问题?

解决方案

作为 mongoDB文档关于 $ lookup :

相同数据库中的未分片集合执行左外部联接,以从联接"集合中过滤文档以进行处理.

所以不,不可能在数据库之间进行.

但是,此外,还存在使用Mongo的 db.getSiblingDB()选项./p>

文档此处其中表示:

用于返回另一个数据库,而无需在shell环境中修改db变量.

因此,这不是 $ lookup ,而是一种获取其他数据库数据的方法.

I have three collections: parent, child1, child2.

parent collection:

[{_id:1}, {_id:2}, {_id:3}]

child1 collection:

[{_id:1, child1:'a', parent:1}, {_id:2, child1:'b', parent:1}, {_id:3, child1:'c', parent:2}]

child2 collection:

[{_id:1, child1:'d', parent:2}, {_id:2, child1:'e', parent:3}]

collections child1 and child2 referenced to parent collection.

now, I want a query in mongoose to get a result like this:

[
 {
  _id:1,
  child1:[{_id:1, child1:'a'}, {_id:2, child1:'b'}],
  child2:[],
 },
 {
  _id:2,
  child1:[{_id:2, child1:'c'}],
  child2:[{_id:1, child1:'d'}],
 },
 {
  _id:3,
  child1:[],
  child2:[{_id:2, child1:'e'}],
 },
]

my try in Aggregate is work correctly:

db.parent.aggregate([
  {
    "$lookup": {
      "from": "child1",
      "localField": "_id",
      "foreignField": "parent",
      "as": "child1"
    }
  },
  {
    "$lookup": {
      "from": "child2",
      "localField": "_id",
      "foreignField": "parent",
      "as": "child2"
    }
  }
])

Mongo Playground

This works when the collections are in the same database but my collections in the separate database.

e.g: parent collection in parent database and child1 and child2 collections in child database.

Do you have any idea for use aggregation across multiple databases or how can I use populate to solve this problem?

解决方案

As mongoDB docs says about $lookup:

Performs a left outer join to an unsharded collection in the same database to filter in documents from the "joined" collection for processing.

So no, it's not possible do it accross DBs.

But, as an addition, using Mongo exists db.getSiblingDB() option.

Docs here where says:

Used to return another database without modifying the db variable in the shell environment.

So, this is not a $lookup but is an approach to have another Data Base data.

这篇关于如何在父母关系中以猫鼬建立一对多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆