Hướng dẫn mongoose connect with username and password nodejs - mongoose kết nối với tên người dùng và mật khẩu nodejs

Tôi có tên người dùng và mật khẩu cho Mongoose, tôi đã sử dụng url này để kết nối MongoDB bằng mongoose

Tôi thử cái này:

var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://adminuser:123456@localhost:2017/mydb');

Lược đồ Mongoose là

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;

Và bộ điều khiển của tôi là

    router.post('/signup',function(req,res){
     console.log("Inside")
     var useR= new User({
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
     });
     useR.save(function(err) {
      if (err) throw err;
      console.log('User saved successfully!');
     });
    });

Nhưng nó không được lưu trữ trong cơ sở dữ liệu.

Kết nối

Chúng tôi có thể kết nối với MongoDB bằng cách sử dụng phương pháp

mongoose.connect('mongodb://localhost/myapp');
8.

mongoose.connect('mongodb://localhost/myapp');

Đây là mức tối thiểu cần thiết để kết nối cơ sở dữ liệu

mongoose.connect('mongodb://localhost/myapp');
9 chạy cục bộ trên cổng mặc định (27017). Chúng tôi cũng có thể chỉ định một số tham số khác trong
mongoose.connect('mongodb://username:password@host:port/database');
0 tùy thuộc vào môi trường của bạn:

mongoose.connect('mongodb://username:password@host:port/database');

Tùy chọn

Phương pháp

mongoose.connect('mongodb://username:password@host:port/database');
1 cũng chấp nhận một đối tượng
mongoose.connect('mongodb://username:password@host:port/database');
2 sẽ được chuyển cho trình điều khiển cơ bản.

mongoose.connect(uri, options);

Các khóa tùy chọn sau đây có sẵn:

 db      - passed to the connection db instance
 server  - passed to the connection server instance(s)
 replset - passed to the connection ReplSet instance
 user    - username for authentication (if not specified in uri)
 pass    - password for authentication (if not specified in uri)

Xem trình điều khiển để biết thêm thông tin về các tùy chọn có sẵn.

Lưu ý: Tùy chọn máy chủ

mongoose.connect('mongodb://username:password@host:port/database');
3 được mặc định là TRUE.

Lưu ý: Tùy chọn DB

mongoose.connect('mongodb://username:password@host:port/database');
4 được đặt thành Sai và không thể bị ghi đè.

Kết nối bản sao

Phương pháp tương tự được sử dụng để kết nối với một bộ bản sao nhưng thay vì chuyển một

mongoose.connect('mongodb://username:password@host:port/database');
0, chúng tôi vượt qua danh sách dấu vết dấu phẩy của
mongoose.connect('mongodb://username:password@host:port/database');
0s.

mongoose.connect('mongodb://username:password@host:port/database,mongodb://username:password@host:port,mongodb://username:password@host:port' [, options]);

LƯU Ý:

mongoose.connect('mongodb://username:password@host:port/database');
7 chỉ cần được chỉ định trong một trong những
mongoose.connect('mongodb://username:password@host:port/database');
0.

Nhiều kết nối

Cho đến nay, chúng tôi đã thấy cách kết nối với MongoDB bằng kết nối mặc định của Mongoose. Đôi khi chúng ta có thể cần nhiều kết nối mở cho Mongo, mỗi cài đặt đọc/ghi khác nhau hoặc có thể chỉ đến các cơ sở dữ liệu khác nhau chẳng hạn. Trong những trường hợp này, chúng tôi có thể sử dụng

mongoose.connect('mongodb://username:password@host:port/database');
9 chấp nhận tất cả các đối số đã được thảo luận và trả về một kết nối mới cho bạn.

var conn = mongoose.createConnection('uri,uri,uri...', options);

Nhóm kết nối

Mỗi

mongoose.connect(uri, options);
0, cho dù được tạo bằng
mongoose.connect(uri, options);
1 hay
mongoose.connect(uri, options);
2 đều được hỗ trợ bởi nhóm kết nối có thể định cấu hình bên trong theo kích thước 5. Điều chỉnh kích thước nhóm bằng các tùy chọn kết nối của bạn:

mongoose.createConnection(uri, { server: { poolSize: 10 }});

Tiếp theo

Bây giờ chúng tôi đã đề cập đến các kết nối, chúng ta hãy xem các mô hình.

Kết nối

Bạn có thể kết nối với MongoDB với phương pháp

mongoose.connect('mongodb://localhost/myapp');
8.

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
0

Đây là mức tối thiểu cần thiết để kết nối cơ sở dữ liệu

mongoose.connect('mongodb://localhost/myapp');
9 chạy cục bộ trên cổng mặc định (27017). Nếu kết nối không thành công trên máy của bạn, hãy thử sử dụng
mongoose.connect(uri, options);
6 thay vì
mongoose.connect(uri, options);
7.

Bạn cũng có thể chỉ định một số tham số khác trong

mongoose.connect('mongodb://username:password@host:port/database');
0:

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
1

Xem thông số chuỗi kết nối MongoDB để biết thêm chi tiết.

  • Bộ đệm
  • Xử lý lỗi
  • Tùy chọn
  • Tùy chọn chuỗi kết nối
  • Sự kiện kết nối
  • Một lưu ý về Keepalive
  • Lựa chọn máy chủ
  • Bản sao đặt kết nối
  • Bản sao đặt tên máy chủ
  • Hỗ trợ nhiều người
  • Nhiều kết nối
  • Nhóm kết nối
  • Thay đổi tùy chọn trong v5.x

Buffering hoạt động

Mongoose cho phép bạn bắt đầu sử dụng các mô hình của mình ngay lập tức, mà không cần chờ Mongoose thiết lập kết nối với MongoDB.

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
2

Đó là bởi vì chức năng mô hình bộ đệm Mongoose gọi nội bộ. Bộ đệm này thuận tiện, nhưng cũng là một nguồn gây nhầm lẫn phổ biến. Mongoose sẽ không ném bất kỳ lỗi nào theo mặc định nếu bạn sử dụng mô hình mà không cần kết nối.

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
3

Để vô hiệu hóa bộ đệm, hãy tắt tùy chọn

mongoose.connect(uri, options);
9 trên lược đồ của bạn. Nếu bạn có
mongoose.connect(uri, options);
9 và kết nối của bạn đang bị treo, hãy thử tắt
mongoose.connect(uri, options);
9 để xem bạn có mở kết nối đúng cách không. Bạn cũng có thể vô hiệu hóa
mongoose.connect(uri, options);
9 trên toàn cầu:

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
4

Lưu ý rằng bộ đệm cũng chịu trách nhiệm chờ đợi cho đến khi Mongoose tạo ra các bộ sưu tập nếu bạn sử dụng tùy chọn

 db      - passed to the connection db instance
 server  - passed to the connection server instance(s)
 replset - passed to the connection ReplSet instance
 user    - username for authentication (if not specified in uri)
 pass    - password for authentication (if not specified in uri)
3. Nếu bạn vô hiệu hóa bộ đệm, bạn cũng nên vô hiệu hóa tùy chọn
 db      - passed to the connection db instance
 server  - passed to the connection server instance(s)
 replset - passed to the connection ReplSet instance
 user    - username for authentication (if not specified in uri)
 pass    - password for authentication (if not specified in uri)
3 và sử dụng
 db      - passed to the connection db instance
 server  - passed to the connection server instance(s)
 replset - passed to the connection ReplSet instance
 user    - username for authentication (if not specified in uri)
 pass    - password for authentication (if not specified in uri)
5 để tạo các bộ sưu tập hoặc bộ sưu tập giới hạn với các đối chiếu.

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
5

Xử lý lỗi

Tùy chọn

  • Tùy chọn chuỗi kết nốinot automatically try to reconnect.
  • Sự kiện kết nối

Một lưu ý về Keepalive

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
6

Lựa chọn máy chủ

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
7

Bản sao đặt kết nối

Tùy chọn

Tùy chọn chuỗi kết nối

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
8

Sự kiện kết nối

  • Một lưu ý về Keepalive
  • Lựa chọn máy chủ
  • Bản sao đặt kết nốiany model associated with this connection.
  • Bản sao đặt tên máy chủ

Hỗ trợ nhiều người

  • Nhiều kết nối
  • Nhóm kết nối
  • Thay đổi tùy chọn trong v5.x
  • Buffering hoạt động
  • mongoose.createConnection(uri, { server: { poolSize: 10 }});
    
    1 - Cho dù kết nối bằng IPv4 hoặc IPv6. Tùy chọn này được chuyển đến hàm
    mongoose.createConnection(uri, { server: { poolSize: 10 }});
    
    2 của Node.js. Nếu bạn không chỉ định tùy chọn này, trình điều khiển MongoDB sẽ thử IPv6 trước và sau đó IPv4 nếu IPv6 không thành công. Nếu cuộc gọi
    mongoose.createConnection(uri, { server: { poolSize: 10 }});
    
    3 của bạn mất nhiều thời gian, hãy thử
    mongoose.createConnection(uri, { server: { poolSize: 10 }});
    
    4
  • mongoose.createConnection(uri, { server: { poolSize: 10 }});
    
    5 - Cơ sở dữ liệu để sử dụng khi xác thực với
    mongoose.connect('mongodb://username:password@host:port/database,mongodb://username:password@host:port,mongodb://username:password@host:port' [, options]);
    
    4 và
    mongoose.connect('mongodb://username:password@host:port/database,mongodb://username:password@host:port,mongodb://username:password@host:port' [, options]);
    
    5. Trong MongoDB, người dùng được đưa vào cơ sở dữ liệu. Nếu bạn đang gặp lỗi đăng nhập bất ngờ, bạn có thể cần đặt tùy chọn này.
  • mongoose.createConnection(uri, { server: { poolSize: 10 }});
    
    8 - Trình điều khiển MongoDB sẽ cố gắng tìm một máy chủ để gửi bất kỳ hoạt động nào đến và tiếp tục thử lại cho
    mongoose.createConnection(uri, { server: { poolSize: 10 }});
    
    8 mili giây. Nếu không được đặt, trình điều khiển MongoDB mặc định sử dụng
    var conn = mongoose.createConnection('uri,uri,uri...', options);
    
    9 (30 giây).
  •   // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    01 - Trình điều khiển MongoDB gửi nhịp tim mỗi
      // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    01 để kiểm tra trạng thái của kết nối. Một nhịp tim phải chịu
    mongoose.createConnection(uri, { server: { poolSize: 10 }});
    
    8, vì vậy người lái xe MongoDB sẽ thử lại các nhịp tim thất bại trong tối đa 30 giây theo mặc định. Mongoose chỉ phát ra một sự kiện
      // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    04 sau khi nhịp tim thất bại, vì vậy bạn có thể muốn giảm cài đặt này để giảm thời gian giữa khi máy chủ của bạn giảm và khi Mongoose phát ra
      // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    04. Chúng tôi khuyên bạn không nên đặt cài đặt này dưới 1000, quá nhiều nhịp tim có thể dẫn đến suy giảm hiệu suất.not set this setting below 1000, too many heartbeats can lead to performance degradation.

Tùy chọn

mongoose.createConnection(uri, { server: { poolSize: 10 }});
8 cũng xử lý thời gian
mongoose.connect('mongodb://localhost/myapp');
8 sẽ thử lại kết nối ban đầu trước khi lỗi.
mongoose.connect('mongodb://localhost/myapp');
8 sẽ thử lại trong 30 giây theo mặc định (mặc định
mongoose.createConnection(uri, { server: { poolSize: 10 }});
8) trước khi lỗi. Để nhận phản hồi nhanh hơn về các hoạt động thất bại, bạn có thể giảm
mongoose.createConnection(uri, { server: { poolSize: 10 }});
8 xuống 5000 như được hiển thị bên dưới.

Example:

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
9

Xem trang này để biết thêm thông tin về

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
11 và
var conn = mongoose.createConnection('uri,uri,uri...', options);
8

Gọi lại

Hàm

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
13 cũng chấp nhận tham số gọi lại và trả về lời hứa.

    router.post('/signup',function(req,res){
     console.log("Inside")
     var useR= new User({
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
     });
     useR.save(function(err) {
      if (err) throw err;
      console.log('User saved successfully!');
     });
    });
0

Tùy chọn chuỗi kết nối

Bạn cũng có thể chỉ định các tùy chọn trình điều khiển trong chuỗi kết nối của mình làm tham số trong phần chuỗi truy vấn của URI. Điều này chỉ áp dụng cho các tùy chọn được chuyển cho trình điều khiển MongoDB. Bạn không thể đặt các tùy chọn dành riêng cho Mongoose như

mongoose.connect(uri, options);
9 trong chuỗi truy vấn.can't set Mongoose-specific options like
mongoose.connect(uri, options);
9 in the query string.

    router.post('/signup',function(req,res){
     console.log("Inside")
     var useR= new User({
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
     });
     useR.save(function(err) {
      if (err) throw err;
      console.log('User saved successfully!');
     });
    });
1

Nhược điểm của việc đặt các tùy chọn vào chuỗi truy vấn là các tùy chọn chuỗi truy vấn khó đọc hơn. Ưu điểm là bạn chỉ cần một tùy chọn cấu hình duy nhất, URI, thay vì các tùy chọn riêng biệt cho

var conn = mongoose.createConnection('uri,uri,uri...', options);
8,
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
11, v.v ... Thực tiễn tốt nhất là đặt các tùy chọn có khả năng khác nhau giữa phát triển và sản xuất, như
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
17 hoặc
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
18, trong chuỗi kết nối, trong chuỗi kết nối, và các tùy chọn vẫn không đổi, như
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
11 hoặc
var conn = mongoose.createConnection('uri,uri,uri...', options);
3, trong đối tượng Tùy chọn.

Các tài liệu MongoDB có một danh sách đầy đủ các tùy chọn chuỗi kết nối được hỗ trợ. Dưới đây là một số tùy chọn thường hữu ích để đặt trong chuỗi kết nối vì chúng được liên kết chặt chẽ với tên máy chủ và thông tin xác thực.

  • mongoose.createConnection(uri, { server: { poolSize: 10 }});
    
    5 - Cơ sở dữ liệu để sử dụng khi xác thực với
    mongoose.connect('mongodb://username:password@host:port/database,mongodb://username:password@host:port,mongodb://username:password@host:port' [, options]);
    
    4 và
    mongoose.connect('mongodb://username:password@host:port/database,mongodb://username:password@host:port,mongodb://username:password@host:port' [, options]);
    
    5. Trong MongoDB, người dùng được đưa vào cơ sở dữ liệu. Nếu bạn đang gặp lỗi đăng nhập bất ngờ, bạn có thể cần đặt tùy chọn này.
  • mongoose.createConnection(uri, { server: { poolSize: 10 }});
    
    1 - Cho dù kết nối bằng IPv4 hoặc IPv6. Tùy chọn này được chuyển đến hàm
    mongoose.createConnection(uri, { server: { poolSize: 10 }});
    
    2 của Node.js. Nếu bạn không chỉ định tùy chọn này, trình điều khiển MongoDB sẽ thử IPv6 trước và sau đó IPv4 nếu IPv6 không thành công. Nếu cuộc gọi
    mongoose.createConnection(uri, { server: { poolSize: 10 }});
    
    3 của bạn mất nhiều thời gian, hãy thử
    mongoose.createConnection(uri, { server: { poolSize: 10 }});
    
    4

Sự kiện kết nối

Các kết nối kế thừa từ lớp

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
28 của Node.js và phát ra các sự kiện khi có điều gì đó xảy ra với kết nối, như mất kết nối với máy chủ MongoDB. Dưới đây là danh sách các sự kiện mà một kết nối có thể phát ra.

  •   // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    29: phát ra khi Mongoose bắt đầu thực hiện kết nối ban đầu với máy chủ MongoDB
  •   // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    30: phát ra khi Mongoose thực hiện thành công kết nối ban đầu với máy chủ MongoDB hoặc khi Mongoose kết nối lại sau khi mất kết nối. Có thể được phát ra nhiều lần nếu Mongoose mất kết nối.
  •   // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    31: phát ra sau
      // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    32 và
      // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    33 được thực thi trên tất cả các mô hình của kết nối này.
  •   // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    34: Ứng dụng của bạn gọi
      // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    35 để ngắt kết nối với MongoDB
  •  db      - passed to the connection db instance
     server  - passed to the connection server instance(s)
     replset - passed to the connection ReplSet instance
     user    - username for authentication (if not specified in uri)
     pass    - password for authentication (if not specified in uri)
    9: phát ra khi Mongoose mất kết nối với máy chủ MongoDB. Sự kiện này có thể là do mã của bạn đóng rõ ràng kết nối, sự cố máy chủ cơ sở dữ liệu hoặc sự cố kết nối mạng.
  •   // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    37: phát ra sau
      // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    35 Đóng thành công kết nối. Nếu bạn gọi
      // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    39, bạn sẽ nhận được cả sự kiện 'bị ngắt kết nối' và sự kiện 'đóng'.
  •   // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    40: phát ra nếu Mongoose mất kết nối với MongoDB và kết nối lại thành công. Mongoose cố gắng tự động kết nối lại khi mất kết nối với cơ sở dữ liệu.
  •   // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    41: phát ra nếu xảy ra lỗi trên kết nối, như
      // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    42 do dữ liệu bị dị tật hoặc tải trọng lớn hơn 16MB.
  •   // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    43: phát ra khi bạn kết nối với một bộ bản sao và Mongoose đã kết nối thành công với chính và ít nhất một phụ.
  •   // grab the things we need
      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
    
      // create a schema
      var userSchema = new Schema({
      name: String,
      username: { type: String, required: true, unique: true },
      password: { type: String, required: true },
      admin: Boolean,
      location: String,
      meta: {
      age: Number,
      website: String
      },
      created_at: Date,
      updated_at: Date
      });
    
      // the schema is useless so far
      // we need to create a model using it
      var User = mongoose.model('User', userSchema);
    
      // make this available to our users in our Node applications
      module.exports = User;
    
    44: phát ra khi bạn kết nối với bộ bản sao và Mongoose đã kết nối thành công với tất cả các máy chủ được chỉ định trong chuỗi kết nối của bạn.

Khi bạn kết nối với một máy chủ MongoDB duy nhất ("độc lập"), Mongoose sẽ phát ra 'ngắt kết nối' nếu nó bị ngắt kết nối với máy chủ độc lập và 'kết nối' nếu nó kết nối thành công với độc lập. Trong một bộ bản sao, Mongoose sẽ phát ra 'ngắt kết nối' nếu nó mất kết nối với bản sao chính và 'kết nối' nếu nó quản lý kết nối lại với bản sao chính.

Một lưu ý về Keepalive

Đối với các ứng dụng chạy dài, thường nên kích hoạt

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
45 với một số mili giây. Không có nó, sau một khoảng thời gian, bạn có thể bắt đầu thấy các lỗi
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
46 cho những gì có vẻ như không có lý do. Nếu vậy, sau khi đọc điều này, bạn có thể quyết định bật
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
45:

    router.post('/signup',function(req,res){
     console.log("Inside")
     var useR= new User({
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
     });
     useR.save(function(err) {
      if (err) throw err;
      console.log('User saved successfully!');
     });
    });
2

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
48 là số mili giây phải chờ trước khi bắt đầu
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
45 trên ổ cắm.
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
45 là đúng theo mặc định vì Mongoose 5.2.0.

Bản sao đặt kết nối

Để kết nối với một bản sao, bạn đã vượt qua danh sách các máy chủ được phân định dấu phẩy để kết nối thay vì một máy chủ duy nhất.

    router.post('/signup',function(req,res){
     console.log("Inside")
     var useR= new User({
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
     });
     useR.save(function(err) {
      if (err) throw err;
      console.log('User saved successfully!');
     });
    });
3

Ví dụ:

    router.post('/signup',function(req,res){
     console.log("Inside")
     var useR= new User({
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
     });
     useR.save(function(err) {
      if (err) throw err;
      console.log('User saved successfully!');
     });
    });
4

Để kết nối với một bộ bản sao nút duy nhất, chỉ định tùy chọn

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
17.

    router.post('/signup',function(req,res){
     console.log("Inside")
     var useR= new User({
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
     });
     useR.save(function(err) {
      if (err) throw err;
      console.log('User saved successfully!');
     });
    });
5

Lựa chọn máy chủ

Trình điều khiển MongoDB cơ bản sử dụng một quy trình được gọi là lựa chọn máy chủ để kết nối với MongoDB và gửi các hoạt động đến MongoDB. Nếu trình điều khiển MongoDB không thể tìm thấy máy chủ để gửi hoạt động đến sau

mongoose.createConnection(uri, { server: { poolSize: 10 }});
8, bạn sẽ nhận được lỗi dưới đây:

    router.post('/signup',function(req,res){
     console.log("Inside")
     var useR= new User({
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
     });
     useR.save(function(err) {
      if (err) throw err;
      console.log('User saved successfully!');
     });
    });
6

Bạn có thể định cấu hình thời gian chờ bằng tùy chọn

mongoose.createConnection(uri, { server: { poolSize: 10 }});
8 để
mongoose.connect('mongodb://localhost/myapp');
8:

    router.post('/signup',function(req,res){
     console.log("Inside")
     var useR= new User({
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
     });
     useR.save(function(err) {
      if (err) throw err;
      console.log('User saved successfully!');
     });
    });
7

Một

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
55 có thuộc tính
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
56 giải thích lý do tại sao lựa chọn máy chủ được định thời. Ví dụ: nếu bạn đang kết nối với máy chủ độc lập với mật khẩu không chính xác,
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
56 sẽ chứa lỗi "xác thực không thành công".

    router.post('/signup',function(req,res){
     console.log("Inside")
     var useR= new User({
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
     });
     useR.save(function(err) {
      if (err) throw err;
      console.log('User saved successfully!');
     });
    });
8

Bản sao đặt tên máy chủ

Bản sao MongoDB dựa vào việc có thể tìm ra tên miền cho mỗi thành viên. Trên Linux và OSX, máy chủ MongoDB sử dụng đầu ra của lệnh

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
58 để tìm ra tên miền để báo cáo cho bộ bản sao. Điều này có thể gây ra các lỗi khó hiểu nếu bạn kết nối với bộ bản sao MongoDB từ xa đang chạy trên máy báo cáo
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
58 của nó là
mongoose.connect(uri, options);
7:

    router.post('/signup',function(req,res){
     console.log("Inside")
     var useR= new User({
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
     });
     useR.save(function(err) {
      if (err) throw err;
      console.log('User saved successfully!');
     });
    });
9

Nếu bạn đang gặp lỗi tương tự, hãy kết nối với bộ bản sao bằng cách sử dụng shell

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
61 và chạy lệnh
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
62 để kiểm tra tên máy chủ của từng thành viên bộ bản sao. Thực hiện theo hướng dẫn của trang này để thay đổi tên máy chủ của thành viên đã đặt bản sao.

Bạn cũng có thể kiểm tra thuộc tính

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
63 của
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
64 để xem trình điều khiển nút MongoDB nghĩ rằng trạng thái của bộ bản sao của bạn là gì. Thuộc tính
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
63 chứa bản đồ mô tả máy chủ.

mongoose.connect('mongodb://localhost/myapp');
0

Hỗ trợ nhiều người

Bạn cũng có thể kết nối với nhiều trường hợp Mongo để có tính sẵn sàng cao trong cụm Sharded. Bạn không cần phải vượt qua bất kỳ tùy chọn đặc biệt nào để kết nối với nhiều Mongo trong Mongoose 5.x.

mongoose.connect('mongodb://localhost/myapp');
1

Nhiều kết nối

Cho đến nay, chúng tôi đã thấy cách kết nối với MongoDB bằng kết nối mặc định của Mongoose. Mongoose tạo kết nối mặc định khi bạn gọi

mongoose.connect('mongodb://localhost/myapp');
8. Bạn có thể truy cập kết nối mặc định bằng
  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;
67.

Bạn có thể cần nhiều kết nối với MongoDB vì nhiều lý do. Một lý do là nếu bạn có nhiều cơ sở dữ liệu hoặc nhiều cụm mongoDB. Một lý do khác là để làm việc xung quanh các chuyến tàu chậm. Hàm

mongoose.connect('mongodb://username:password@host:port/database');
9 có các đối số giống như
mongoose.connect('mongodb://localhost/myapp');
8 và trả về một kết nối mới.

mongoose.connect('mongodb://localhost/myapp');
2

Đối tượng kết nối này sau đó được sử dụng để tạo và truy xuất các mô hình. Các mô hình luôn được đưa vào một kết nối duy nhất.always scoped to a single connection.

mongoose.connect('mongodb://localhost/myapp');
3

Nếu bạn sử dụng nhiều kết nối, bạn nên đảm bảo rằng bạn xuất các lược đồ, không phải các mô hình. Xuất một mô hình từ một tệp được gọi là mẫu mô hình xuất. Mẫu mô hình xuất bị hạn chế vì bạn chỉ có thể sử dụng một kết nối.not models. Exporting a model from a file is called the export model pattern. The export model pattern is limited because you can only use one connection.

mongoose.connect('mongodb://localhost/myapp');
4

Nếu bạn sử dụng mẫu lược đồ xuất, bạn vẫn cần tạo các mô hình ở đâu đó. Có hai mẫu phổ biến. Đầu tiên là xuất kết nối và đăng ký các mô hình trên kết nối trong tệp:

mongoose.connect('mongodb://localhost/myapp');
5

Một cách khác là đăng ký các kết nối với một kim phun phụ thuộc hoặc một mô hình đảo ngược khác của điều khiển (IOC).

mongoose.connect('mongodb://localhost/myapp');
6

Nhóm kết nối

Mỗi

mongoose.connect(uri, options);
0, cho dù được tạo bằng
mongoose.connect(uri, options);
1 hay
mongoose.connect(uri, options);
2 đều được hỗ trợ bởi nhóm kết nối có thể cấu hình bên trong mặc định ở kích thước tối đa là 100. Điều chỉnh kích thước nhóm bằng các tùy chọn kết nối của bạn:

mongoose.connect('mongodb://localhost/myapp');
7

Tiếp theo

Bây giờ chúng tôi đã đề cập đến các kết nối, chúng ta hãy xem các mô hình.