nodejs mongodb增删改查grud操作代码

代码语言:nodejs

所属分类:其他

代码描述:nodejs mongodb增删改查grud操作代码

代码标签: 删改 grud 操作

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
 
// Connection URL
const url = 'mongodb://localhost:27017';
 
// Database Name
const dbName = 'koa';
console.time('start');
// Use connect method to connect to the server
MongoClient.connect(url,{useUnifiedTopology: true},function(err, client) {
    assert.equal(null, err);
    console.log("MongoDB server Connected successfully to server");
    const db = client.db(dbName);
 
    insertDocuments(db,function () {
        findDocuments(db, function () {
            removeDocument(db,function () {
                updateDocument(db,function () {
                    client.close();
                });
 
            });
        });
    })
});
 
const insertDocuments = function(db, callback) {
    // Get the documents collection
    const collection = db.collection('user');
    // Insert some documents
    collection.insertMany([
        {username : "hemeili"   ,age : "22",sex : "女" , status : "0" },
        {username : "doujinmin" ,age : "32",sex : "男" , status : "1" },
        {username : "yanghexing",age : "19",sex : "女" , status : "0" },
    ], function(err, result) {
        assert.equal(err, null);
        assert.equal(3, result.result.n);
        assert.equal(3, result.ops.length);
        console.log("Inserted 3 documents into the collection&.........完整代码请登录后点击上方下载按钮下载查看

网友评论0