尝试使用 `gatsby-source-mongodb` 将数据从 MongoDB 提取到 Gatsby 中
Stuck trying to fetch data from MongoDB into Gatsby using `gatsby-source-mongodb`
所以似乎几乎没有关于如何做到这一点的详细文档。基本上我所需要指导的就是this。我已经跟进到 映射媒体类型功能 ,此时我陷入了困境。它给出了一个例子:为了使用 markdown 从集合中获取数据。但这不是我要实现的目标。
基本上我想做的是从 Mongo 导入几个集合,我打算将它们导入我的 Gatsby 应用程序——可能使用 GraphQL。
到目前为止,这就是我对 gatsby-config.js
:
所做的
{
resolve: `gatsby-source-mongodb`,
options: {
dbName: `REDACTED`,
collection: `articles`,
map: { articles: { /* WHAT DO I DO HERE? */ } }
},
server: { address: `REDACTED`, port: 43532 },
auth: { user: `REDACTED`, password: `REDACTED` },
extraParams: { replicaSet: `test-shard-0`, ssl: true, authSource: `admin` }
}
在此之后我需要找到一种查询导入集合的方法,但我真的不知道该怎么做。
非常感谢您提供的任何帮助!
文档看起来很清楚。
Basically what I want to do is import a couple of collections from Mongo,
// In your gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-source-mongodb`,
options: { dbName: `local`, collection: [`documents`, `vehicles`] },
},
],
}
After this I then need find a way of querying the imported collection
query {
allMongodbLocalDocuments {
edges {
node {
id
url
name
}
}
}
}
注意:根据您的 dbName
,查询 allMongodbLocalDocuments
可能因您的情况而异。但是您应该能够在 GraphiQL (http://localhost:8000/___graphql
)
中找到可用的查询
现在您应该可以在 GraphiQL 中测试上述查询了。
然后,看看 gatsby using mongodb example 项目。在 gatsby-node.js
中,他们使用上述查询,然后从 ./src/templates/item.js
处的模板中为查询中的每个节点创建一个页面
首先,我的问题中的一个主要错误是我将 server
、auth
和 extraParams
放在了 options
对象之外。
以下配置使我能够使 Atlas 正常工作:
{
resolve: `gatsby-source-mongodb`,
options: {
dbName: `your-database-name`,
collection: [`yourCollection1`, `yourCollection2`],
server: { address: 'cluster0-shard-00-01-XXXX.mongodb.net', port: 27017},
auth: { user: 'yourUserName', password: 'yourPassword' },
extraParams: { replicaSet: 'Cluster0-shard-0', ssl: true, authSource: `admin`, retryWrites: true }
}
}
注意:replicaSet 参数必须与您地址的开头相匹配(如示例中所示)。您还可以通过单击集群主信息面板 cloud.mongodb.com 上的 CONNECT
按钮找到您的集群 URL。 (下图)
所以似乎几乎没有关于如何做到这一点的详细文档。基本上我所需要指导的就是this。我已经跟进到 映射媒体类型功能 ,此时我陷入了困境。它给出了一个例子:为了使用 markdown 从集合中获取数据。但这不是我要实现的目标。
基本上我想做的是从 Mongo 导入几个集合,我打算将它们导入我的 Gatsby 应用程序——可能使用 GraphQL。
到目前为止,这就是我对 gatsby-config.js
:
{
resolve: `gatsby-source-mongodb`,
options: {
dbName: `REDACTED`,
collection: `articles`,
map: { articles: { /* WHAT DO I DO HERE? */ } }
},
server: { address: `REDACTED`, port: 43532 },
auth: { user: `REDACTED`, password: `REDACTED` },
extraParams: { replicaSet: `test-shard-0`, ssl: true, authSource: `admin` }
}
在此之后我需要找到一种查询导入集合的方法,但我真的不知道该怎么做。
非常感谢您提供的任何帮助!
文档看起来很清楚。
Basically what I want to do is import a couple of collections from Mongo,
// In your gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-source-mongodb`,
options: { dbName: `local`, collection: [`documents`, `vehicles`] },
},
],
}
After this I then need find a way of querying the imported collection
query {
allMongodbLocalDocuments {
edges {
node {
id
url
name
}
}
}
}
注意:根据您的 dbName
,查询 allMongodbLocalDocuments
可能因您的情况而异。但是您应该能够在 GraphiQL (http://localhost:8000/___graphql
)
现在您应该可以在 GraphiQL 中测试上述查询了。
然后,看看 gatsby using mongodb example 项目。在 gatsby-node.js
中,他们使用上述查询,然后从 ./src/templates/item.js
首先,我的问题中的一个主要错误是我将 server
、auth
和 extraParams
放在了 options
对象之外。
以下配置使我能够使 Atlas 正常工作:
{
resolve: `gatsby-source-mongodb`,
options: {
dbName: `your-database-name`,
collection: [`yourCollection1`, `yourCollection2`],
server: { address: 'cluster0-shard-00-01-XXXX.mongodb.net', port: 27017},
auth: { user: 'yourUserName', password: 'yourPassword' },
extraParams: { replicaSet: 'Cluster0-shard-0', ssl: true, authSource: `admin`, retryWrites: true }
}
}
注意:replicaSet 参数必须与您地址的开头相匹配(如示例中所示)。您还可以通过单击集群主信息面板 cloud.mongodb.com 上的 CONNECT
按钮找到您的集群 URL。 (下图)