mongodb 3.x以上版本与mongodb 2.x版本语法区别

时间:2020-02-07 15:22:49   收藏:0   阅读:93

2.x

const MongoClient = require(‘mongodb‘).MongoClient;
const url = ‘mongodb://localhost:27017/test‘;
MongoClient.connect(url, function (err, db) {
if (err) throw new Error(err);
db.collection(‘myCollection‘).updateOne({ // Update method ‘updateOne‘
greetings: "Hellu" },
{ $set: { greetings: "Whut?" }},
function (err, result) {
if (err) throw new Error(err);
db.close(); // Don‘t forget to close the connection when you are done
});
});

3.x

const MongoClient = require(‘mongodb‘).MongoClient;

const url = ‘mongodb://localhost:27017/wang‘;

MongoClient.connect(url, function(err, client) {
    if(err) throw new Error(err);
    var db = client.db(‘wang‘);
    db.collection(‘myCollection‘).updateOne({ // Update method ‘updateOne‘
    greetings: "Hellu" },{ $set: {greetings: "Whut?"}},
                        function(err,result) {
                        if(err) throw new Error(err);
                        client.close(); // Don‘t forget to close the connection when you are done
                        });
});
                        
                        
                       

 

原文:https://www.cnblogs.com/lishidefengchen/p/12272688.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!