MongoDB
The MongoDB connector allows you to connect one or more MongoDB Atlas Databases using the Data API to Grafbase.
The MongoDB connector accepts the following config:
name
(required) — The unique name for the data sourceurl
(required) — The MongoDB Atlas API URLapiKey
(required) — The MongoDB Atlas API KeydataSource
(required) — The MongoDB Atlas datasourcedatabase
(required) — The MongoDB Atlas database
import { config, connector, graph } from '@grafbase/sdk'
const g = graph.Standalone()
const mongo = connector.MongoDB('MongoDB', {
url: g.env('MONGODB_API_URL'),
apiKey: g.env('MONGODB_API_KEY'),
dataSource: g.env('MONGODB_DATASOURCE'),
database: g.env('MONGODB_DATABASE'),
})
g.datasource(mongo)
export default config({
graph: g,
})
The url
for Mongo must be for the Data API and start with https://
, not mongodb://
.
Grafbase automatically generates a GraphQL API to read and write to MongoDB Atlas using the models you define in the schema configuration:
const address = g
.type('Address', {
street: g.string(),
city: g.string(),
country: g.string(),
})
.collection('addresses')
mongo
.model('User', {
name: g.string(),
age: g.int().optional(),
address: g.ref(address).optional(),
metadata: g.json().optional(),
})
.collection('users')
Models can also specify different collection name, and default values:
mongo
.model('Author', {
name: g.string(),
likes: g.int().default(0),
})
.collection('users')