Hướng dẫn insertone mongodb nodejs - insertone mongodb nodejs

Tài liệu về nhà → node.jsNode.js

Show

    Bạn có thể chèn một tài liệu vào một bộ sưu tập bằng phương thức Collection.insertone (). Để chèn một tài liệu, xác định một đối tượng chứa các trường và giá trị mà bạn muốn lưu trữ. Nếu bộ sưu tập được chỉ định không tồn tại, phương thức insertOne() sẽ tạo ra bộ sưu tập.collection.insertOne() method. To insert a document, define an object that contains the fields and values that you want to store. If the specified collection does not exist, the insertOne() method creates the collection.

    Bạn có thể chỉ định các tùy chọn truy vấn bổ sung bằng tham số options. Để biết thêm thông tin về các tham số phương thức, hãy xem tài liệu API chèn (). Để biết thêm thông tin về phương thức này, hãy xem tài liệu API chèn ().insertOne() API documentation. For more information on this method, see the insertOne() API documentation.

    Nếu thao tác chèn thành công một tài liệu, nó sẽ nối một trường insertedId vào đối tượng được chuyển trong lệnh gọi phương thức và đặt giá trị của trường vào _id của tài liệu được chèn.

    Ghi chú

    Bạn có thể sử dụng ví dụ này để kết nối với một thể hiện của MongoDB và tương tác với cơ sở dữ liệu có chứa dữ liệu mẫu. Để tìm hiểu thêm về việc kết nối với thể hiện MongoDB của bạn và tải một bộ dữ liệu mẫu, hãy xem Hướng dẫn sử dụng ví dụ.

    Nếu bạn chạy ví dụ trước, bạn sẽ thấy đầu ra sau:

    A document was inserted with the _id:

    Tài liệu về nhà → node.jsNode.js

    Ghi chú

    Ví dụ này kết nối với một thể hiện của MongoDB và sử dụng cơ sở dữ liệu dữ liệu mẫu. Để tìm hiểu thêm về việc kết nối với thể hiện MongoDB của bạn và tải cơ sở dữ liệu này, hãy xem Hướng dẫn sử dụng ví dụ.API documentation for information on the result object.

    Tài liệu về nhà → node.jscollection.insertOne() method. To insert a document, define an object that contains the fields and values that you want to store. If the specified collection does not exist, the insertOne() method creates the collection.

    Nếu bạn chỉ định một phương thức gọi lại, insertOne() không trả về không có gì. Nếu bạn không chỉ định một, phương thức này sẽ trả về một Promise giải quyết cho đối tượng kết quả khi hoàn thành. Xem hướng dẫn của chúng tôi về lời hứa và cuộc gọi lại để biết thêm thông tin hoặc tài liệu API để biết thông tin về đối tượng kết quả.insertOne() API documentation. You can also pass a callback method as an optional third parameter. For more information on this method, see the insertOne() API documentation.

    Trường insertedId của đối tượng này là _id của tài liệu được chèn. Nếu thao tác không tạo tài liệu, trường

    1const { MongoClient } = require("mongodb");
    2
    3// Replace the uri string with your MongoDB deployment's connection string.
    4const uri =
    5 "mongodb+srv://:@?writeConcern=majority";
    6
    7const client = new MongoClient(uri, {
    8 useNewUrlParser: true,
    9 useUnifiedTopology: true,
    10});
    11
    12async function run() {
    13 try {
    14 await client.connect();
    15
    16 const database = client.db("sample_mflix");
    17 const movies = database.collection("movies");
    18 // create a document to be inserted
    19 const doc = { name: "Red", town: "kanto" };
    20 const result = await movies.insertOne(doc);
    21
    22 console.log(
    23 `${result.insertedCount} documents were inserted with the _id: ${result.insertedId}`,
    24 );
    25 } finally {
    26 await client.close();
    27 }
    28}
    29run().catch(console.dir);
    2 của đối tượng này có giá trị
    1const { MongoClient } = require("mongodb");
    2
    3// Replace the uri string with your MongoDB deployment's connection string.
    4const uri =
    5 "mongodb+srv://:@?writeConcern=majority";
    6
    7const client = new MongoClient(uri, {
    8 useNewUrlParser: true,
    9 useUnifiedTopology: true,
    10});
    11
    12async function run() {
    13 try {
    14 await client.connect();
    15
    16 const database = client.db("sample_mflix");
    17 const movies = database.collection("movies");
    18 // create a document to be inserted
    19 const doc = { name: "Red", town: "kanto" };
    20 const result = await movies.insertOne(doc);
    21
    22 console.log(
    23 `${result.insertedCount} documents were inserted with the _id: ${result.insertedId}`,
    24 );
    25 } finally {
    26 await client.close();
    27 }
    28}
    29run().catch(console.dir);
    3. Nếu thao tác tạo tài liệu, trường
    1const { MongoClient } = require("mongodb");
    2
    3// Replace the uri string with your MongoDB deployment's connection string.
    4const uri =
    5 "mongodb+srv://:@?writeConcern=majority";
    6
    7const client = new MongoClient(uri, {
    8 useNewUrlParser: true,
    9 useUnifiedTopology: true,
    10});
    11
    12async function run() {
    13 try {
    14 await client.connect();
    15
    16 const database = client.db("sample_mflix");
    17 const movies = database.collection("movies");
    18 // create a document to be inserted
    19 const doc = { name: "Red", town: "kanto" };
    20 const result = await movies.insertOne(doc);
    21
    22 console.log(
    23 `${result.insertedCount} documents were inserted with the _id: ${result.insertedId}`,
    24 );
    25 } finally {
    26 await client.close();
    27 }
    28}
    29run().catch(console.dir);
    2 của đối tượng này có giá trị
    1const { MongoClient } = require("mongodb");
    2
    3// Replace the uri string with your MongoDB deployment's connection string.
    4const uri =
    5 "mongodb+srv://:@?writeConcern=majority";
    6
    7const client = new MongoClient(uri, {
    8 useNewUrlParser: true,
    9 useUnifiedTopology: true,
    10});
    11
    12async function run() {
    13 try {
    14 await client.connect();
    15
    16 const database = client.db("sample_mflix");
    17 const movies = database.collection("movies");
    18 // create a document to be inserted
    19 const doc = { name: "Red", town: "kanto" };
    20 const result = await movies.insertOne(doc);
    21
    22 console.log(
    23 `${result.insertedCount} documents were inserted with the _id: ${result.insertedId}`,
    24 );
    25 } finally {
    26 await client.close();
    27 }
    28}
    29run().catch(console.dir);
    5.

    Ghi chú

    Ví dụ này kết nối với một thể hiện của MongoDB và sử dụng cơ sở dữ liệu dữ liệu mẫu. Để tìm hiểu thêm về việc kết nối với thể hiện MongoDB của bạn và tải cơ sở dữ liệu này, hãy xem Hướng dẫn sử dụng ví dụ.

    1const { MongoClient } = require("mongodb");
    2
    3// Replace the uri string with your MongoDB deployment's connection string.
    4const uri =
    5 "mongodb+srv://:@?writeConcern=majority";
    6
    7const client = new MongoClient(uri, {
    8 useNewUrlParser: true,
    9 useUnifiedTopology: true,
    10});
    11
    12async function run() {
    13 try {
    14 await client.connect();
    15
    16 const database = client.db("sample_mflix");
    17 const movies = database.collection("movies");
    18 // create a document to be inserted
    19 const doc = { name: "Red", town: "kanto" };
    20 const result = await movies.insertOne(doc);
    21
    22 console.log(
    23 `${result.insertedCount} documents were inserted with the _id: ${result.insertedId}`,
    24 );
    25 } finally {
    26 await client.close();
    27 }
    28}
    29run().catch(console.dir);