相当于 Dynamoose 中的 Schema.Types.ObjectId
Equivalent of Schema.Types.ObjectId in Dynamoose
我正在使用 Node 和 React 创建一个片段管理器,作为一个学习项目,我们可以在我工作的地方使用它。
我之前使用 Express、Mongoose 和 mLab 设置了它。带着学习和挑战自我的心态,我想转向使用 AWS 的 DynamoDB。
我找到了 Dynaamoose,这将是一个很大的帮助,因为 API 几乎与 Mongoose 相同。但是,我似乎无法弄清楚如何重新创建 Schema.Types.ObjectId 所做的事情。
const SnippetSchema = new Schema({
id: {
type: String,
default: shortid.generate(),
hashKey: true
},
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
category: {
type: Schema.Types.ObjectId,
ref: 'categories'
},
code: {
type: String,
required: true
},
title: {
type: String,
required: true
}});
它的格式不完美,但本质上我如何让它以同样的方式引用我的用户和我的类别?
与 Mongoose 和 MongoDB 不同,Dynamoose 和 DynamoDB 没有默认的 _id 属性。因此 DynamoDB/Dynamoose.
中没有 Schema.Types.ObjectId
这样的东西
看起来你想要做的是 .populate
。
就我个人而言,我只是手动处理所有关联,使用 .get
。
在上面的架构示例中,id
属性 将是您的主键。在 DynamoDB 中,主键必须是唯一的,否则它们将覆盖现有项目(除非您进行一些检查)。
从这里我认为您可以构建一个系统来为您的每个文档生成 ID,然后通过该 ID 将其关联起来。我通常为此 属性 使用字符串,但我认为它在技术上可以是任何受支持的 DynamoDB 属性 类型。
希望这有助于您入门。如果它没有意义或您需要更多帮助,请随时发表评论。作为 Dynamoose GitHub Project 的成员,我非常活跃,所以请随时在他们的问题上创建问题或内容。我尝试每天检查一次 Stack Overflow 上的 Dynamoose
标签。我的联系方式也很好找。
祝你迁移到 Dynamoose 好运,如果你 运行 遇到任何其他问题或有任何其他问题,请告诉我!!
我猜你想要这样:
const SnippetSchema = new Schema({
id: {
type: String,
default: shortid.generate(),
hashKey: true
},
parent: User,
category:{
type: String,
required: true,
index: {
global: true
}
},
code: {
type: String,
required: true
},
title: {
type: String,
required: true
}
});
所以当 .get(id)
将return所有具有名为“parent”的对象的数据,其中包含用户数据
我正在使用 Node 和 React 创建一个片段管理器,作为一个学习项目,我们可以在我工作的地方使用它。
我之前使用 Express、Mongoose 和 mLab 设置了它。带着学习和挑战自我的心态,我想转向使用 AWS 的 DynamoDB。
我找到了 Dynaamoose,这将是一个很大的帮助,因为 API 几乎与 Mongoose 相同。但是,我似乎无法弄清楚如何重新创建 Schema.Types.ObjectId 所做的事情。
const SnippetSchema = new Schema({
id: {
type: String,
default: shortid.generate(),
hashKey: true
},
user: {
type: Schema.Types.ObjectId,
ref: 'users'
},
category: {
type: Schema.Types.ObjectId,
ref: 'categories'
},
code: {
type: String,
required: true
},
title: {
type: String,
required: true
}});
它的格式不完美,但本质上我如何让它以同样的方式引用我的用户和我的类别?
与 Mongoose 和 MongoDB 不同,Dynamoose 和 DynamoDB 没有默认的 _id 属性。因此 DynamoDB/Dynamoose.
中没有Schema.Types.ObjectId
这样的东西
看起来你想要做的是 .populate
。
就我个人而言,我只是手动处理所有关联,使用 .get
。
在上面的架构示例中,id
属性 将是您的主键。在 DynamoDB 中,主键必须是唯一的,否则它们将覆盖现有项目(除非您进行一些检查)。
从这里我认为您可以构建一个系统来为您的每个文档生成 ID,然后通过该 ID 将其关联起来。我通常为此 属性 使用字符串,但我认为它在技术上可以是任何受支持的 DynamoDB 属性 类型。
希望这有助于您入门。如果它没有意义或您需要更多帮助,请随时发表评论。作为 Dynamoose GitHub Project 的成员,我非常活跃,所以请随时在他们的问题上创建问题或内容。我尝试每天检查一次 Stack Overflow 上的 Dynamoose
标签。我的联系方式也很好找。
祝你迁移到 Dynamoose 好运,如果你 运行 遇到任何其他问题或有任何其他问题,请告诉我!!
我猜你想要这样:
const SnippetSchema = new Schema({
id: {
type: String,
default: shortid.generate(),
hashKey: true
},
parent: User,
category:{
type: String,
required: true,
index: {
global: true
}
},
code: {
type: String,
required: true
},
title: {
type: String,
required: true
}
});
所以当 .get(id)
将return所有具有名为“parent”的对象的数据,其中包含用户数据