Py学习  »  MongoDB

MongoDB子到父关系

TheAmplifier • 4 年前 • 160 次点击  

我创建了一个meteor应用程序,其中有两个集合,activitycards和users。我的ActivityCard集合中有一个对用户的引用,如下所示:

 {
    "_id" : "T9QwsHep3dMSRWNTK",
    "cardName" : "Guntnauna",
    "activitycardType" : 10,
    "startDate" : "1952-08-09",
    "remainingHours" : 0,
    "activities" : [ 
      {
        "activityName" : "Cfuw",
        "activityTotal" : "5",
        "activityEmployee" : "Smamnktl",
        "activityDate" : "1960-07-16"
      }, 

      ... 
    ],
    "customerID" : "z9hXczmWaf7wgdAtj",
    "isArchived" : 0
}

customerID包含以下用户集合的ID:

{
    "_id" : "9mXAZkmfpKMPvQY8Y",
    "createdAt" : ISODate(),
    "services" : {
      "password" : {
        "bcrypt" : ""
      }
    },
    "emails" : [ 
      {
        "address" : "topony@mailinator.com",
        "verified" : false
      }
    ],
    "profile" : {
      "relationNumber" : "962",
      "companyName" : "Snider Kinney Co",
      "locationStreet" : "Ullamco eaque consequatur aspernatur consectetur eiusmod eligendi enim rerum consectetur asperiores officia eius itaque expedita dolorum",
      "locationPostal" : "Sit inventore asperiores est anim commodo non fugiat consequat Voluptatem tempore sunt culpa magni",
      "locationCity" : "Ipsum et fugit pariatur Nobis eveniet neque veniam perferendis eius ut quo excepteur consequatur voluptatem architecto",
      "contactName" : "Julian Moran",
      "contactPhone" : "+388-14-8339022",
      "isArchived" : 0
    },
    "roles" : {
      "customers" : [ 
        "customer"
      ]
    }
} 

我是mongodb新手,不知道这是否是文档中正确配置的关系,我主要是从父到子找到关系,而不是相反。

我想知道你们对这种关系有什么建议。 如何保存它们,从两个集合中获取数据,等等。

如果我的代码可以使用,我想知道如何从孩子到家长的关系,并显示他们。目前我只使用MongoDB中的find()方法,并将数据映射到单独的值中。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40258
 
160 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Ana Lava
Reply   •   1 楼
Ana Lava    5 年前

你的MongoDB集合中可以有这样的关系。如果使用Mongoose,可以在模式中定义它们,如下所示:

customerId:{{ type: mongoose.Schema.ObjectId, ref: 'user' }}

你可以通过 mongoose populate . 有关详细信息,请参见

https://mongoosejs.com/docs/populate.html