Javascript MongoDB node.js insertOne正在插入两个文档
我正在运行一个来自MongoDB文档的非常简单的示例。我调用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
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);