返回
Featured image of post MongoDB - 深入Insert

MongoDB - 深入Insert

MongoDB - 想寫入?懷念關聯表?

基本使用

insertOne - 新增單筆

// ...略
interface User {
  name: string;
  age: number;
  gender: "male" | "female";
}
const collection: Collection<User> = db.collection<User>("user");
let { insertedId } = await collection.insertOne({
  name: "Fred",
  age: 27,
  gender: "male",
});

insertMany - 新增多筆

interface User {
  name: string;
  age: number;
  gender: "male" | "female";
}
const collection: Collection<User> = db.collection<User>("user");
let { insertedId } = await collection.insertOne([
  {
    name: "Fred",
    age: 27,
    gender: "male",
  },
  {
    name: "Chichi",
    age: 26,
    gender: "female",
  }
]);
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus