gpt4 book ai didi

node.js - 如何为子文档创建 mongoDb 模式

转载 作者:行者123 更新时间:2023-12-01 23:59:54 25 4
gpt4 key购买 nike

我有如下文档。这里 peoples 包含指向用户集合的 ObjectId 数组。并且 contribution 字段包含与 peoples 字段一样多的子文档。长度是可变的,如果某个组在 people 中有 2 个 ObjectId,那么 contribution 将有 2 个子文档。我需要为此创建 mongoDb 模式,请告诉我这个模式。

{
name: "person name",
_id: ObjectId(""),
creater: ObjectId("1"), //referencing to user collection
peoples: [ObjectId("1"), ObjectId("2"),...upto n], //all referencing to user table
contribution: {
ObjectId("1"):{
paid: 1200,
due: 1000,
prevDue: 200,
Advance: 0
},
ObjectId("2"):{
paid: 1200,
due: 1000,
prevDue: 200,
Advance: 0
},
//upto end of lists in peoples array
},
estimated: 30000,
collected: 15379,
left: 14721
}

最佳答案

您只需在主 schema 中引用嵌套模式。例如:

let user = new Schema({
name: String
})

let schema = new Schema({
followers: [user]
})

schemafollowers 字段中,您刚刚引用了 user 模式。每当完成这种引用时,嵌套模式的类型就会被注入(inject)到引用点中。

关于node.js - 如何为子文档创建 mongoDb 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62012244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com