Insert
insertOne
import { MongoClient, ObjectId } from "mongodb";
const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true&w=majority";
const client = new MongoClient(uri);
interface Pokemon {
_id?: ObjectId,
name: string,
age: number
}
async function main() {
try {
// Connect to the MongoDB cluster
await client.connect();
let pikachu: Pokemon = { name:"pikachu", age:12 };
const result = await client.db("Les").collection("pokemon").insertOne(pikachu);
console.log(`New document created with the following id: ${result.insertedId}`);
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
main();insertMany
Laatst bijgewerkt