xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

MongoDB Node.js Driver and MongoClient All In One

MongoDB Node.js Driver and MongoClient All In One

The next generation Node.js driver for MongoDB

$ npm i mongodb
# OR
$ npm i -S mongodb
# OR
$ npm install mongodb --save

https://mongodb.github.io/node-mongodb-native/index.html

https://www.mongodb.com/docs/drivers/node/current/

https://learn.mongodb.com/learning-paths/using-mongodb-with-nodejs

https://mongodb.github.io/node-mongodb-native/Next/

image

The URL connection format

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

// new MongoClient

https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html#the-url-connection-format

https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html

demos

var MongoClient = require('mongodb').MongoClient;

// MongoClient.connect ⚠️ callback function
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, db) {
  test.equal(null, err);
  test.ok(db != null);

  db.collection("replicaset_mongo_client_collection").update({a:1}, {b:1}, {upsert:true}, function(err, result) {
    test.equal(null, err);
    test.equal(1, result);

    db.close();
    test.done();
  });
});

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Grid = require('mongodb').Grid,
    Code = require('mongodb').Code,
    BSON = require('mongodb').pure().BSON,
    assert = require('assert');

  // Set up the connection to the local db ✅ Promise
  var mongoclient = new MongoClient(new Server("localhost", 27017), {native_parser: true});

  // Open the connection to the server
  mongoclient.open(function(err, mongoclient) {

    // Get the first db and do an update document on it
    var db = mongoclient.db("integration_tests");
    db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
      assert.equal(null, err);
      assert.equal(1, result);

      // Get another db and do an update document on it
      var db2 = mongoclient.db("integration_tests2");
      db2.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
        assert.equal(null, err);
        assert.equal(1, result);

        // Close the connection
        mongoclient.close();
      });
    });
  });

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

Tutorials

https://learn.mongodb.com/

https://learn.mongodb.com/catalog?labels=["Learning Format"]&values=["Learning Path"]

https://learn.mongodb.com/learning-paths/mongodb-nodejs-developer-path

free videos

https://learn.mongodb.com/courses/connecting-to-mongodb-in-nodejs

https://learn.mongodb.com/courses/mongodb-aggregation-with-nodejs

https://learn.mongodb.com/courses/mongodb-crud-operations-in-nodejs

Connecting to an Atlas Cluster in Node.js Applications https://learn.mongodb.com/learn/course/connecting-to-mongodb-in-nodejs/lesson-2-connecting-to-an-atlas-cluster-in-nodejs-applications/first-lesson?client=customer&wvideo=rigink8xd1

https://embed-ssl.wistia.com/deliveries/08ab815abd61fe06f98e5bd97c34d5533f794cd2.mp4?filename=Connecting to an Atlas Cluster in Node.js Applications.mp4&disposition=attachment

image
image

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

/* 

mongoose 底层依赖于 mongodb

https://www.npmjs.com/package/mongoose

*/

const uri = `mongodb://127.0.0.1:27017/so`;

const client = new MongoClient(uri);

const dbName = `wikis`;

const connectToDatabase = async () => {
  try {
    await client.connect();
    console.log(`${dbName} connect ✅`);
  } catch (err) {
    console.log(`${dbName} connect ❌`, err);
  }
}

const main = async () => {
  try {
    await connectToDatabase();
    console.log(`${dbName} connect ✅`);
  } catch (err) {
    // 
    console.log(`${dbName} connect ❌`, err);
  } finally {
    await client.close();
    console.log(`${dbName} close 🎉`);
  }
}

main();


refs

https://www.mongodb.com/docs/drivers/node/current/fundamentals/connection/
https://www.mongodb.com/docs/drivers/node/current/fundamentals/authentication/
https://www.mongodb.com/docs/drivers/node/current/fundamentals/crud/



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-10-10 12:57  xgqfrms  阅读(3)  评论(2编辑  收藏  举报