Xử lý lỗi mongodb

Trước MongoDB 3. 6, theo dõi oplog MongoDB là cách duy nhất để lắng nghe các thay đổi đối với cơ sở dữ liệu MongoDB của bạn. oplog là một bộ sưu tập đặc biệt ghi lại tất cả các phần chèn và bản cập nhật đi vào máy chủ MongoDB của bạn để các thành viên khác của bộ bản sao có thể sao chép chúng. Các công cụ như Meteor và MoSQL được xây dựng dựa trên việc theo dõi oplog. Thật không may, oplog được xây dựng chủ yếu để hỗ trợ sao chép nội bộ chứ không phải là một tính năng dành cho người dùng. Các luồng thay đổi là một lớp khả năng sử dụng trên oplog MongoDB giúp phát hiện thay đổi trong MongoDB dễ dàng hơn nhiều so với theo dõi trực tiếp oplog

Luồng thay đổi đầu tiên của bạn

Thay đổi luồng yêu cầu bộ bản sao MongoDB. Bạn sẽ gặp lỗi nếu cố mở một phiên bản dựa trên phiên bản MongoDB độc lập. Đối với bài viết này, tôi sẽ sử dụng Node mongodb-topology-manager. js để khởi động bộ bản sao MongoDB, do đó bạn không phải tự mình thiết lập một bản sao. Gói mongodb-topology-manager rất phù hợp để thử nghiệm và các tập lệnh mẫu, nhưng không sử dụng nó cho ứng dụng sản xuất của bạn. Nếu bạn muốn có một bộ bản sao MongoDB sẵn sàng sản xuất và không muốn tự quản lý nó, tôi khuyên bạn nên sử dụng MongoDB Atlas

Khi bạn đã thiết lập bộ bản sao, bạn sẽ có thể tạo luồng thay đổi bằng phương pháp

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
9 trên bộ sưu tập trình điều khiển MongoDB hoặc mô hình Mongoose

Dưới đây là tập lệnh độc lập

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
0 khởi động bộ bản sao MongoDB, tạo luồng thay đổi và chèn tài liệu để kích hoạt luồng thay đổi. Yêu cầu duy nhất để chạy tập lệnh này là một
$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
1 trong PATH của bạn, Nút. js và đang chạy
$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
2

Đầu ra của việc chạy tập lệnh trên trông như thế này

$ node changestream.js
2018-02-09T17:48:03.986Z 'start'
2018-02-09T17:48:18.704Z 'Replica set started...'
2018-02-09T17:48:18.751Z 'Inserting doc'
2018-02-09T17:48:18.780Z 'Inserted doc'
2018-02-09T17:48:18.815Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7ddee248096515af26a46b, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7ddee248096515af26a46b } }
^C
$

Thay đổi dữ liệu luồng có một số thuộc tính quan trọng

  • $ node changestream.js
    2018-02-09T18:01:50.366Z 'start'
    2018-02-09T18:02:03.555Z 'Replica set started...'
    2018-02-09T18:02:03.609Z 'Inserting doc'
    2018-02-09T18:02:03.632Z 'Inserted doc'
    [ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
    2018-02-09T18:02:03.675Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
    2018-02-09T18:02:03.679Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
    ^C
    $
    0. phải là một trong các 'chèn', 'cập nhật', 'thay thế', 'xóa' hoặc 'vô hiệu hóa'. Xem thông số kỹ thuật này. Khác với 'không hợp lệ', những điều này tương ứng với các thao tác CRUD của MongoDB theo cách rõ ràng,
    $ node changestream.js
    2018-02-09T18:01:50.366Z 'start'
    2018-02-09T18:02:03.555Z 'Replica set started...'
    2018-02-09T18:02:03.609Z 'Inserting doc'
    2018-02-09T18:02:03.632Z 'Inserted doc'
    [ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
    2018-02-09T18:02:03.675Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
    2018-02-09T18:02:03.679Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
    ^C
    $
    1 trở thành 'chèn',
    $ node changestream.js
    2018-02-09T18:01:50.366Z 'start'
    2018-02-09T18:02:03.555Z 'Replica set started...'
    2018-02-09T18:02:03.609Z 'Inserting doc'
    2018-02-09T18:02:03.632Z 'Inserted doc'
    [ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
    2018-02-09T18:02:03.675Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
    2018-02-09T18:02:03.679Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
    ^C
    $
    2 trở thành 'cập nhật',
    $ node changestream.js
    2018-02-09T18:01:50.366Z 'start'
    2018-02-09T18:02:03.555Z 'Replica set started...'
    2018-02-09T18:02:03.609Z 'Inserting doc'
    2018-02-09T18:02:03.632Z 'Inserted doc'
    [ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
    2018-02-09T18:02:03.675Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
    2018-02-09T18:02:03.679Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
    ^C
    $
    3 trở thành 'thay thế' và
    $ node changestream.js
    2018-02-09T18:01:50.366Z 'start'
    2018-02-09T18:02:03.555Z 'Replica set started...'
    2018-02-09T18:02:03.609Z 'Inserting doc'
    2018-02-09T18:02:03.632Z 'Inserted doc'
    [ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
    2018-02-09T18:02:03.675Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
    2018-02-09T18:02:03.679Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
    ^C
    $
    4 trở thành 'xóa'. Tương tự như vậy,
    $ node changestream.js
    2018-02-09T18:01:50.366Z 'start'
    2018-02-09T18:02:03.555Z 'Replica set started...'
    2018-02-09T18:02:03.609Z 'Inserting doc'
    2018-02-09T18:02:03.632Z 'Inserted doc'
    [ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
    2018-02-09T18:02:03.675Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
    2018-02-09T18:02:03.679Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
    ^C
    $
    5 được dịch sang nhiều sự kiện 'chèn' trong luồng thay đổi
  • $ node changestream.js
    2018-02-09T18:01:50.366Z 'start'
    2018-02-09T18:02:03.555Z 'Replica set started...'
    2018-02-09T18:02:03.609Z 'Inserting doc'
    2018-02-09T18:02:03.632Z 'Inserted doc'
    [ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
    2018-02-09T18:02:03.675Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
    2018-02-09T18:02:03.679Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
    ^C
    $
    6. tên cơ sở dữ liệu và bộ sưu tập, thay đổi này xảy ra vào
  • $ node changestream.js
    2018-02-09T18:01:50.366Z 'start'
    2018-02-09T18:02:03.555Z 'Replica set started...'
    2018-02-09T18:02:03.609Z 'Inserting doc'
    2018-02-09T18:02:03.632Z 'Inserted doc'
    [ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
    2018-02-09T18:02:03.675Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
    2018-02-09T18:02:03.679Z { _id:
       { _data:
          Binary {
            _bsontype: 'Binary',
            sub_type: 0,
            position: 49,
            buffer:  } },
      operationType: 'insert',
      fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
      ns: { db: 'test', coll: 'Person' },
      documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
    ^C
    $
    7. trạng thái hiện tại của tài liệu tại thời điểm xảy ra thao tác này. Thêm về điều này sau

Các hoạt động ảnh hưởng đến nhiều tài liệu, như

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
5, tạo một sự kiện 'thay đổi' cho mỗi tài liệu bị ảnh hưởng. Ví dụ: nếu bạn gọi
$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
5 bằng 2 tài liệu, bạn sẽ nhận được hai sự kiện 'thay đổi'. Nói chung, mỗi sự kiện 'thay đổi' mô tả các thay đổi đối với một tài liệu

Dưới đây là đầu ra với các thay đổi ở trên thành

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
0

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$

Cập nhật và Tùy chọn $ node changestream.js 2018-02-09T18:01:50.366Z 'start' 2018-02-09T18:02:03.555Z 'Replica set started...' 2018-02-09T18:02:03.609Z 'Inserting doc' 2018-02-09T18:02:03.632Z 'Inserted doc' [ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' }, { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ] 2018-02-09T18:02:03.675Z { _id: { _data: Binary { _bsontype: 'Binary', sub_type: 0, position: 49, buffer: } }, operationType: 'insert', fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' }, ns: { db: 'test', coll: 'Person' }, documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } } 2018-02-09T18:02:03.679Z { _id: { _data: Binary { _bsontype: 'Binary', sub_type: 0, position: 49, buffer: } }, operationType: 'insert', fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' }, ns: { db: 'test', coll: 'Person' }, documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } } ^C $7

Tài liệu thay đổi để chèn và thay thế rất đơn giản vì

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
7, tài liệu mà bạn đang chèn, sẽ luôn hiện diện. Các bản cập nhật phức tạp hơn một chút, theo mặc định,
$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
43 có nghĩa là
$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
7 sẽ không có mặt. Thay vào đó, sẽ có một thuộc tính
$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
45 mô tả các thay đổi đối với tài liệu. Ví dụ: bên dưới là một
$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
0 đã sửa đổi để chèn một tài liệu và sau đó thực hiện một
$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
2 trên tài liệu

Đầu ra trông giống như những gì bạn nhìn thấy bên dưới

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
4

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
45 rất hữu ích, nhưng thường thì bạn cũng sẽ muốn trạng thái hiện tại của tài liệu. Để nhận thuộc tính
$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
7 cho các bản cập nhật, bạn cần đặt tùy chọn
$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
7 trên hàm
$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
9 thành chuỗi 'updateLookup'

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
2

Tài liệu đầu ra được hiển thị bên dưới. Lưu ý rằng

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
7 chứa tài liệu sau khi áp dụng bản cập nhật

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
4

Thay đổi luồng trong Mongoose

cầy mangut 5. x có hỗ trợ cơ bản cho các luồng thay đổi. Hiện tại, cầy mangut không cung cấp bất kỳ tính năng đặc biệt nào trên các luồng thay đổi ngoài chức năng

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
23. Dưới đây là tệp
$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
0 ban đầu sử dụng Mongoose thay vì trình điều khiển MongoDB

Đầu ra, hiển thị bên dưới, giống như ví dụ về trình điều khiển MongoDB. Mongoose hiện không thực hiện bất kỳ quá trình truyền đặc biệt nào với các sự kiện 'thay đổi'

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
6

tiếp tục

Luồng thay đổi là tính năng mới nổi bật nhất trong MongoDB 3. 6, nhưng chúng không phải là duy nhất. Bộ lọc mảng cung cấp cho bạn các công cụ tinh vi để cập nhật mảng. Khung tổng hợp có nhiều toán tử và giai đoạn mới. Đảm bảo bạn nâng cấp và dùng thử tất cả các tính năng mới này, chỉ cần nhớ rằng trước tiên bạn nâng cấp trình điều khiển MongoDB lên

$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
25 hoặc Mongoose lên
$ node changestream.js
2018-02-09T18:01:50.366Z 'start'
2018-02-09T18:02:03.555Z 'Replica set started...'
2018-02-09T18:02:03.609Z 'Inserting doc'
2018-02-09T18:02:03.632Z 'Inserted doc'
[ { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' } ]
2018-02-09T18:02:03.675Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d659, name: 'Axl Rose' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d659 } }
2018-02-09T18:02:03.679Z { _id:
   { _data:
      Binary {
        _bsontype: 'Binary',
        sub_type: 0,
        position: 49,
        buffer:  } },
  operationType: 'insert',
  fullDocument: { _id: 5a7de21ba1a72d1c38d7d65a, name: 'Slash' },
  ns: { db: 'test', coll: 'Person' },
  documentKey: { _id: 5a7de21ba1a72d1c38d7d65a } }
^C
$
26