我是 mongoDb 的新手,上了一门课程,但停留在这一点上。我创建了这个 Index.js,在这里我插入一个数据并使用回调,但插入后它反映的是未定义?
--indes.js
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const url = 'mongodb://localhost:27017/';
const dbname = 'conFusion';
MongoClient.connect(url, (err, client) => {
assert.equal(err, null);
console.log('Connected correctly to server');
const db = client.db(dbname);
const collection = db.collection('dishes');
collection.insertOne({ "name": "Uthappizza1", "description": "test" },
(err, result) => {
assert.equal(err, null);
console.log('After Insert:\n');
console.log(result.ops);
collection.find({}).toArray((err, docs) => {
assert.equal(err, null);
console.log('Found:\n');
console.log(docs);
db.dropCollection('dishes', (err, result) => {
assert.equal(err, null);
client.close();
});
});
});
});
also attaching the package.json for more clearilty
{
"name": "node-mongo",
"version": "1.0.0",
"description": "Node MongoDB Example",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index"
},
"author": "Shivam Singh",
"license": "ISC",
"dependencies": {
"assert": "^2.0.0",
"mongodb": "^4.0.1"
}
}
