Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MongoDB node.js insertOne正在插入两个文档_Javascript_Node.js_Database_Mongodb - Fatal编程技术网

Javascript MongoDB node.js insertOne正在插入两个文档

Javascript MongoDB node.js insertOne正在插入两个文档,javascript,node.js,database,mongodb,Javascript,Node.js,Database,Mongodb,我正在运行一个来自MongoDB文档的非常简单的示例。我调用insertOne插入一个文档,但它在集合中插入了两个文档。无法理解为什么会发生这种行为。代码如下: const dbURI = "mongodb+srv://user:password@clusteruri.mongodb.net/?w=majority"; const { MongoClient } = require("mongodb"); const client = new Mo

我正在运行一个来自MongoDB文档的非常简单的示例。我调用insertOne插入一个文档,但它在集合中插入了两个文档。无法理解为什么会发生这种行为。代码如下:

const dbURI =
  "mongodb+srv://user:password@clusteruri.mongodb.net/?w=majority";

const { MongoClient } = require("mongodb");

const client = new MongoClient(dbURI, { useUnifiedTopology: true });

async function run() {
  try {
    await client.connect();

    const database = client.db("test");
    const collection = database.collection("testcollection");
    // create a document to be inserted
    const doc = { name: "Red", town: "kanto" };
    const result = await collection.insertOne(doc);

    console.log(
      `${result.insertedCount} documents were inserted with the _id: ${result.insertedId}`
    );
  } finally {
    await client.close();
  }
}
run().catch(console.dir);