As Roman, points out in his response, it is not ideal to do that.
However, you can leverage a getStaticProps
to fetch the documents you need from your database. If you were rendering dynamically the classic use case of use profiles, it'd prolly look like the following pseudo-code, and assuming that you've got some sort of Model
to interface your MongoDb:
// under app/pages/users/[userId].js
import UserProfile from 'components/user-profile';
import User from 'models/user';
export default UserProfile;
// both at request time and build time, preps the props passed to the UserProfile component.
export const getStaticProps = async ({params}) => {
const user = await User.find(params.id);
return {
props: { user }
}
}
Bonus track: if your use case supports it, turning this into a statically generated site it's pretty straight-forward:
// instructs next to render all user profiles using SSG
export const getStaticPaths = async () => {
const users = await User.findAll();
const paths = users.map(user => `/users/${user.id}`);
return { paths, fallback: false };
}