我正在建立一个个人博客,并选择盖茨比,因为明显的原因(表现和容易开始),因为我有一些反应的正面背景。此外,我还构建了一个简单的应用程序来创建我的内容(html字符串),并使用快捷服务器存储在MongoDB数据库中。现在对于博客来说,我只是尝试使用MongoDB插件从gatsby-source-mongodb中提取数据。
我的MongoDB模式有关系。例如,一个'Post‘模式有一个' user’属性,它是一个从'User‘模式中引用用户的ObjectID。我的gatsby-source-mongodb配置如下:
{
resolve: 'gatsby-source-mongodb',
options: {
dbName: 'KathaDB',
collection: 'posts',
server: {
address: "somecluster",
port: 27017
},
auth: {
user: 'someuser',
password: 'somepasswd'
},
extraParams: {
replicaSet: 'test',
ssl: true,
authSource: 'admin',
retryWrites: true,
preserveObjectIds: true
}
}
}我有几个问题:
发布于 2020-05-27 06:18:55
确实如此。
MongoDB的关系依赖于ObjectID,因此您可能不得不将preserveObjectIds: true添加到插件选项中:
{
resolve: "gatsby-source-mongodb",
options: {
dbName: "KathaDB",
collection: "posts",
server: {
address: "somecluster",
port: 27017,
},
auth: {
user: "someuser",
password: "somepasswd",
},
extraParams: {
replicaSet: "test",
ssl: true,
authSource: "admin",
retryWrites: true,
preserveObjectIds: true,
},
preserveObjectIds: true, // <= here
},
};我不确定gatsby-source-mongodb是否会凭空创建关系(如果我的内存是正确的,我不认为是这样的),但是使用ObjectIds,您可以使用GraphQL创建外键关系。
在盖茨比,有两种方法可以做到这一点:
gatsby-config.js我推荐第二个选项,因为它是一种更GraphQL的方式,并且发生在大多数节点操作都发生在gatsby-node.js中。但是,如果您从盖茨比和GraphQL开始,第一个选项可能更容易设置。
https://stackoverflow.com/questions/62035677
复制相似问题