Hướng dẫn nodejs mongodb cursor - con trỏ nodejs mongodb

Con trỏ()¶

Người xây dựng¶

Constructor cho một đối tượng con trỏ xử lý tất cả các hoạt động trên kết quả truy vấn bằng tìm kiếm. Đối tượng con trỏ này là đơn hướng và không thể đi qua phía sau. Khách hàng không nên tạo một con trỏ trực tiếp, nhưng sử dụng tìm để có được con trỏ. (Loại nội bộ)

Lớp con trỏ ()

Arguments:
  • DB (DB) - Đối tượng cơ sở dữ liệu để làm việc. (db) – the database object to work with.
  • Bộ sưu tập (Bộ sưu tập) - Bộ sưu tập để truy vấn. (collection) – the collection to query.
  • bộ chọn (đối tượng) - Bộ chọn truy vấn. (object) – the query selector.
  • Các trường (đối tượng) - Một đối tượng chứa những trường nào sẽ bao gồm hoặc loại trừ khỏi các đối tượng được trả về. (object) – an object containing what fields to include or exclude from objects returned.
  • [Tùy chọn] (Đối tượng) - Tùy chọn bổ sung cho bộ sưu tập. (object) – additional options for the collection.

Tùy chọn
  • Bỏ qua {number} Bỏ qua số lượng tài liệu để bỏ qua. {Number} skip number of documents to skip.
  • Giới hạn {số}, giới hạn số lượng kết quả để trả về. -1 có một ý nghĩa đặc biệt và được sử dụng bởi db.eval. Giá trị 1 cũng sẽ được đối xử như thể nó là -1. {Number}, limit the number of results to return. -1 has a special meaning and is used by Db.eval. A value of 1 will also be treated as if it were -1.
  • Sắp xếp {mảng | Đối tượng}, được đặt để sắp xếp các tài liệu trở lại từ truy vấn. Mảng các chỉ mục, [[‘A, 1]], v.v. {Array | Object}, set to sort the documents coming back from the query. Array of indexes, [[‘a’, 1]] etc.
  • Gợi ý {Object}, Gợi ý buộc truy vấn sử dụng một chỉ mục cụ thể. {Object}, hint force the query to use a specific index.
  • Giải thích {boolean}, giải thích trả về giải thích của truy vấn. {Boolean}, explain return the explaination of the query.
  • Snapshot {Boolean}, Chế độ chụp nhanh đảm bảo không có bản sao nào được trả về. {Boolean}, snapshot Snapshot mode assures no duplicates are returned.
  • Thời gian chờ {boolean}, thời gian chờ cho phép truy vấn hết thời gian chờ. {Boolean}, timeout allow the query to timeout.
  • Đuôi có thể {boolean}, đuôi có thể cho phép con trỏ có thể có được đuôi. {Boolean}, tailable allow the cursor to be tailable.
  • Awaitdata {Boolean}, AwaitData cho phép con trỏ chờ dữ liệu, chỉ áp dụng cho con trỏ có thể đuôi. {Boolean}, awaitdata allow the cursor to wait for data, only applicable for tailable cursor.
  • oplogreplay {boolean}, đặt một cờ bên trong, chỉ áp dụng cho con trỏ có thể điều chỉnh được. {Boolean}, sets an internal flag, only applicable for tailable cursor.
  • BatchSize {number}, hàng loạt số lượng tập hợp con kết quả để yêu cầu cơ sở dữ liệu trả về cho mọi yêu cầu. Điều này ban đầu sẽ lớn hơn 1 nếu không cơ sở dữ liệu sẽ tự động đóng con trỏ. Kích thước lô có thể được đặt thành 1 với cursorinstance.batchsize sau khi thực hiện truy vấn ban đầu vào cơ sở dữ liệu. {Number}, batchSize the number of the subset of results to request the database to return for every request. This should initially be greater than 1 otherwise the database will automatically close the cursor. The batch size can be set to 1 with cursorInstance.batchSize after performing the initial query to the database.
  • Raw {Boolean}, RAW RETURN TẤT CẢ tài liệu truy vấn dưới dạng bộ đệm thô (mặc định sai). {Boolean}, raw return all query documents as raw buffers (default false).
  • Đọc {boolean}, đọc Chỉ định ghi đè của đọc từ nguồn (chính/phụ). {Boolean}, read specify override of read from source (primary/secondary).
  • ReturnKey {Boolean}, ReturnKey chỉ trả về phím chỉ mục. {Boolean}, returnKey only return the index key.
  • MaxScan {Number}, MaxScan giới hạn số lượng mục cần quét. {Number}, maxScan limit the number of items to scan.
  • tối thiểu {number}, min đặt giới hạn chỉ mục. {Number}, min set index bounds.
  • Max {number}, giới hạn chỉ mục đặt tối đa. {Number}, max set index bounds.
  • MaxTimems {number}, số giây để chờ đợi trước khi hủy bỏ truy vấn. {Number}, number of miliseconds to wait before aborting the query.
  • showdiskloc {boolean}, showdiskloc hiển thị vị trí đĩa kết quả. {Boolean}, showDiskLoc show disk location of results.
  • Nhận xét {String}, Nhận xét Bạn có thể đặt một trường bình luận $ trên một truy vấn để làm cho việc tìm kiếm các nhật ký hồ sơ đơn giản hơn. {String}, comment you can put a $comment field on a query to make looking in the profiler logs simpler.
  • NumberOfretries {number}, numberOfretries nếu sử dụng Awaidata chỉ định số lần để thử lại khi hết thời gian chờ. {Number}, numberOfRetries if using awaidata specifies the number of times to retry on timeout.
  • DbName {String}, DbName ghi đè DBName mặc định. {String}, dbName override the default dbName.
  • TailableretryInterval {number}, TailableTeretyInterval Chỉ định các milisecond giữa getMores trên con trỏ có thể điều chỉnh được. {Number}, tailableRetryInterval specify the miliseconds between getMores on tailable cursor.
  • Ống xả {boolean}, ống xả có máy chủ gửi tất cả các tài liệu cùng một lúc dưới dạng các gói GetMore. {Boolean}, exhaust have the server send all the documents at once as getMore packets.
  • Một phần {Boolean}, một phần có hệ thống Sharded trả về kết quả một phần từ Mongo. {Boolean}, partial have the sharded system return a partial result from mongos.

Hằng số trong

Tên liên tụcGiá trịSự mô tả
Con trỏ.init0 Tiểu bang
Con trỏ.open1 Con trỏ mở
Con trỏ.Closed2 Con trỏ đóng cửa
Con trỏ.get_more3 Con trỏ thực hiện nhận được nhiều hơn

Con trỏ.clonewithoptions¶

Bản sao một con trỏ nhất định nhưng sử dụng các tùy chọn mới

Con trỏ.clonewithoptions (con trỏ) ¶
Arguments:
  • con trỏ (con trỏ) - con trỏ đến bản sao. (cursor) – the cursor to clone.
Returns:

Đối tượng [Tùy chọn] Tùy chọn bổ sung cho bộ sưu tập khi nhân bản.

tua lại

Đặt lại con trỏ này về trạng thái ban đầu của nó. Tất cả các cài đặt như chuỗi truy vấn, đuôi, BatchSizeValue, SkipValue và giới hạn được bảo tồn.

tua lại ()
Returns:Con trỏ tự trả lại với tua lại được áp dụng.

Ví dụ

Một ví dụ hiển thị thông tin được trả về bởi IndexInformation

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

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {
  var docs = [];

  // Insert 100 documents with some data
  for(var i = 0; i < 100; i++) {
    var d = new Date().getTime() + i*1000;
    docs[i] = {'a':i, createdAt:new Date(d)};
  }

  // Create collection
  db.createCollection('Should_correctly_rewind_and_restart_cursor', function(err, collection) {
    assert.equal(null, err);

    // insert all docs
    collection.insert(docs, {w:1}, function(err, result) {
      assert.equal(null, err);

      // Grab a cursor using the find
      var cursor = collection.find({});
      // Fetch the first object off the cursor
      cursor.nextObject(function(err, item) {
        assert.equal(0, item.a)
        // Rewind the cursor, resetting it to point to the start of the query
        cursor.rewind();

        // Grab the first object again
        cursor.nextObject(function(err, item) {
          assert.equal(0, item.a)

          db.close();
        })
      })
    })
  });
});

toarray¶

Trả về một mảng các tài liệu. Người gọi chịu trách nhiệm đảm bảo rằng có đủ bộ nhớ để lưu trữ kết quả. Lưu ý rằng mảng chỉ chứa kết quả một phần khi con trỏ này được truy cập trước. Trong trường hợp đó, con trỏ.rewind () có thể được sử dụng để đặt lại con trỏ.

toarray (gọi lại) ¶
Arguments:
  • Callback (Function) - Điều này sẽ được gọi sau khi thực hiện phương thức này thành công. Tham số đầu tiên sẽ chứa đối tượng lỗi nếu xảy ra lỗi hoặc null khác. Tham số thứ hai sẽ chứa một mảng các đối tượng khử Deserialized do kết quả của truy vấn. (function) – This will be called after executing this method successfully. The first parameter will contain the Error object if an error occured, or null otherwise. The second parameter will contain an array of BSON deserialized objects as a result of the query.
Returns:

null

Ví dụ

Một ví dụ hiển thị thông tin được trả về bởi IndexInformation

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

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {

  // Create a collection to hold our documents
  db.createCollection('test_array', function(err, collection) {

    // Insert a test document
    collection.insert({'b':[1, 2, 3]}, {w:1}, function(err, ids) {

      // Retrieve all the documents in the collection
      collection.find().toArray(function(err, documents) {
        assert.equal(1, documents.length);
        assert.deepEqual([1, 2, 3], documents[0].b);

        db.close();
      });
    });
  });
});

toarray¶

Trả về một mảng các tài liệu. Người gọi chịu trách nhiệm đảm bảo rằng có đủ bộ nhớ để lưu trữ kết quả. Lưu ý rằng mảng chỉ chứa kết quả một phần khi con trỏ này được truy cập trước. Trong trường hợp đó, con trỏ.rewind () có thể được sử dụng để đặt lại con trỏ.{cursor.toArray}, not all of the elements will be iterated if this cursor had been previouly accessed. In that case, {cursor.rewind} can be used to reset the cursor. However, unlike {cursor.toArray}, the cursor will only hold a maximum of batch size elements at any given time if batch size is specified. Otherwise, the caller is responsible for making sure that the entire result can fit the memory.

Mỗi (gọi lại)
Arguments:
  • Gọi lại (hàm) - Điều này sẽ được gọi trong khi lặp lại mọi tài liệu của kết quả truy vấn. Tham số đầu tiên sẽ chứa đối tượng lỗi nếu xảy ra lỗi hoặc null khác. Trong khi tham số thứ hai sẽ chứa tài liệu. (function) – this will be called for while iterating every document of the query result. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the document.
Returns:

null

Ví dụ

Một ví dụ đơn giản lặp lại trên một truy vấn bằng cách sử dụng từng hàm của con trỏ.

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

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {

  // Create a collection
  db.createCollection('test_to_a_after_each', function(err, collection) {
    assert.equal(null, err);

    // Insert a document in the collection
    collection.insert({'a':1}, {w:1}, function(err, ids) {

      // Grab a cursor
      var cursor = collection.find();

      // Execute the each command, triggers for each document
      cursor.each(function(err, item) {

        // If the item is null then the cursor is exhausted/empty and closed
        if(item == null) {

          // Show that the cursor is closed
          cursor.toArray(function(err, items) {
            assert.ok(err != null);

            // Let's close the db
            db.close();
          });
        };
      });
    });
  });
});

đếm¶

Xác định xem có bao nhiêu kết quả truy vấn cho con trỏ này sẽ trở lại

Đếm (Ứng dụngkiplimit, gọi lại)
Arguments:
  • Ứng dụngkiplimit (boolean) - nếu được đặt thành true sẽ áp dụng bỏ qua và giới hạn đặt trên con trỏ. Mặc định là sai. (boolean) – if set to true will apply the skip and limits set on the cursor. Defaults to false.
  • Gọi lại (hàm) - Điều này sẽ được gọi sau khi thực hiện phương thức này. Tham số đầu tiên sẽ chứa đối tượng lỗi nếu xảy ra lỗi hoặc null khác. Trong khi tham số thứ hai sẽ chứa số lượng kết quả hoặc null nếu xảy ra lỗi. (function) – this will be called after executing this method. The first parameter will contain the Error object if an error occured, or null otherwise. While the second parameter will contain the number of results or null if an error occured.
Returns:

null

Ví dụ

Một ví dụ đơn giản lặp lại trên một truy vấn bằng cách sử dụng từng hàm của con trỏ.

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

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {

  // Creat collection
  db.createCollection('cursor_count_collection', function(err, collection) {
    assert.equal(null, err);

    // Insert some docs
    collection.insert([{a:1}, {a:2}], {w:1}, function(err, docs) {
      assert.equal(null, err);

      // Do a find and get the cursor count
      collection.find().count(function(err, count) {
        assert.equal(null, err);
        assert.equal(2, count);

        db.close();
      })
    });
  });
});

đếm¶

Xác định xem có bao nhiêu kết quả truy vấn cho con trỏ này sẽ trở lại

Đếm (Ứng dụngkiplimit, gọi lại)

Ứng dụngkiplimit (boolean) - nếu được đặt thành true sẽ áp dụng bỏ qua và giới hạn đặt trên con trỏ. Mặc định là sai.
Arguments:
  • Gọi lại (hàm) - Điều này sẽ được gọi sau khi thực hiện phương thức này. Tham số đầu tiên sẽ chứa đối tượng lỗi nếu xảy ra lỗi hoặc null khác. Trong khi tham số thứ hai sẽ chứa số lượng kết quả hoặc null nếu xảy ra lỗi. (string) – This can be a string or an array. If passed as a string, the string will be the field to sort. If passed an array, each element will represent a field to be sorted and should be an array that contains the format [string, direction].
  • Một ví dụ đơn giản cho thấy chức năng đếm của con trỏ. (string) – this determines how the results are sorted. "asc", "ascending" or 1 for asceding order while "desc", "desceding or -1 for descending order. Note that the strings are case insensitive.
  • loại¶ (function) – this will be called after executing this method. The first parameter will contain an error object when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
Returns:

Đặt tham số sắp xếp của con trỏ này thành giá trị đã cho.

Ví dụ

Một ví dụ đơn giản lặp lại trên một truy vấn bằng cách sử dụng từng hàm của con trỏ.

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

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {

  // Create a collection
  db.createCollection('simple_sort_collection', function(err, collection) {
    assert.equal(null, err);

    // Insert some documents we can sort on
    collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, docs) {
      assert.equal(null, err);

      // Do normal ascending sort
      collection.find().sort([['a', 1]]).nextObject(function(err, item) {
        assert.equal(null, err);
        assert.equal(1, item.a);

        // Do normal descending sort
        collection.find().sort([['a', -1]]).nextObject(function(err, item) {
          assert.equal(null, err);
          assert.equal(3, item.a);

          db.close();
        });
      });
    });
  });
});

đếm¶

Xác định xem có bao nhiêu kết quả truy vấn cho con trỏ này sẽ trở lại

Đếm (Ứng dụngkiplimit, gọi lại)[, callback])
Arguments:
  • Ứng dụngkiplimit (boolean) - nếu được đặt thành true sẽ áp dụng bỏ qua và giới hạn đặt trên con trỏ. Mặc định là sai. (number) – the new limit.
  • Gọi lại (hàm) - Điều này sẽ được gọi sau khi thực hiện phương thức này. Tham số đầu tiên sẽ chứa đối tượng lỗi nếu xảy ra lỗi hoặc null khác. Trong khi tham số thứ hai sẽ chứa số lượng kết quả hoặc null nếu xảy ra lỗi. (function) – this optional callback will be called after executing this method. The first parameter will contain an error object when the limit given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
Returns:

Đặt tham số sắp xếp của con trỏ này thành giá trị đã cho.

Ví dụ

Phương thức này có chữ ký phương thức sau: (Danh sách chính, gọi lại) (Danh sách chính, hướng, gọi lại)

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

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {

  // Create a collection
  db.createCollection('simple_limit_collection', function(err, collection) {
    assert.equal(null, err);

    // Insert some documents we can sort on
    collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, docs) {
      assert.equal(null, err);

      // Limit to only one document returned
      collection.find().limit(1).toArray(function(err, items) {
        assert.equal(null, err);
        assert.equal(1, items.length);

        db.close();
      });
    });
  });
});

Sắp xếp (Danh sách chính, hướng, gọi lại)

KeyorList (chuỗi) - Đây có thể là một chuỗi hoặc một mảng. Nếu được truyền dưới dạng chuỗi, chuỗi sẽ là trường sắp xếp. Nếu được truyền một mảng, mỗi phần tử sẽ biểu thị một trường được sắp xếp và phải là một mảng chứa định dạng [chuỗi, hướng].

Hướng (chuỗi) - Điều này xác định cách sắp xếp kết quả. & quot; Asc & quot ;, & quot; Ascending & quot; hoặc 1 cho đơn đặt hàng trong khi & quot; desc & quot ;, & quot; giảm dần hoặc -1 cho đơn đặt hàng giảm dần. Lưu ý rằng các chuỗi là trường hợp không nhạy cảm.[, callback]
Arguments:
  • Gọi lại (hàm) - Điều này sẽ được gọi sau khi thực hiện phương thức này. Tham số đầu tiên sẽ chứa một đối tượng lỗi khi con trỏ đã được đóng trong khi tham số thứ hai sẽ chứa tham chiếu đến đối tượng này khi thực hiện thành công. (number) – the maxTimeMS for the query.
  • Gọi lại (hàm) - Điều này sẽ được gọi sau khi thực hiện phương thức này. Tham số đầu tiên sẽ chứa đối tượng lỗi nếu xảy ra lỗi hoặc null khác. Trong khi tham số thứ hai sẽ chứa số lượng kết quả hoặc null nếu xảy ra lỗi. (function) – this optional callback will be called after executing this method. The first parameter will contain an error object when the limit given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
Returns:

Đặt tham số sắp xếp của con trỏ này thành giá trị đã cho.

Phương thức này có chữ ký phương thức sau: (Danh sách chính, gọi lại) (Danh sách chính, hướng, gọi lại)

Sắp xếp (Danh sách chính, hướng, gọi lại)

KeyorList (chuỗi) - Đây có thể là một chuỗi hoặc một mảng. Nếu được truyền dưới dạng chuỗi, chuỗi sẽ là trường sắp xếp. Nếu được truyền một mảng, mỗi phần tử sẽ biểu thị một trường được sắp xếp và phải là một mảng chứa định dạng [chuỗi, hướng].[, callback]
Arguments:
  • Hướng (chuỗi) - Điều này xác định cách sắp xếp kết quả. & quot; Asc & quot ;, & quot; Ascending & quot; hoặc 1 cho đơn đặt hàng trong khi & quot; desc & quot ;, & quot; giảm dần hoặc -1 cho đơn đặt hàng giảm dần. Lưu ý rằng các chuỗi là trường hợp không nhạy cảm. (string) – read preference for the cursor, one of Server.READ_PRIMARY, Server.READ_SECONDARY, Server.READ_SECONDARY_ONLY
  • Gọi lại (hàm) - Điều này sẽ được gọi sau khi thực hiện phương thức này. Tham số đầu tiên sẽ chứa một đối tượng lỗi khi con trỏ đã được đóng trong khi tham số thứ hai sẽ chứa tham chiếu đến đối tượng này khi thực hiện thành công. (function) – this optional callback will be called after executing this method. The first parameter will contain an error object when the read preference given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
Returns:

Đặt tham số sắp xếp của con trỏ này thành giá trị đã cho.

Phương thức này có chữ ký phương thức sau: (Danh sách chính, gọi lại) (Danh sách chính, hướng, gọi lại)

Sắp xếp (Danh sách chính, hướng, gọi lại)

KeyorList (chuỗi) - Đây có thể là một chuỗi hoặc một mảng. Nếu được truyền dưới dạng chuỗi, chuỗi sẽ là trường sắp xếp. Nếu được truyền một mảng, mỗi phần tử sẽ biểu thị một trường được sắp xếp và phải là một mảng chứa định dạng [chuỗi, hướng].[, callback])
Arguments:
  • Hướng (chuỗi) - Điều này xác định cách sắp xếp kết quả. & quot; Asc & quot ;, & quot; Ascending & quot; hoặc 1 cho đơn đặt hàng trong khi & quot; desc & quot ;, & quot; giảm dần hoặc -1 cho đơn đặt hàng giảm dần. Lưu ý rằng các chuỗi là trường hợp không nhạy cảm. (number) – the new skip value.
  • Gọi lại (hàm) - Điều này sẽ được gọi sau khi thực hiện phương thức này. Tham số đầu tiên sẽ chứa một đối tượng lỗi khi con trỏ đã được đóng trong khi tham số thứ hai sẽ chứa tham chiếu đến đối tượng này khi thực hiện thành công. (function) – this optional callback will be called after executing this method. The first parameter will contain an error object when the skip value given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
Returns:

Đặt tham số sắp xếp của con trỏ này thành giá trị đã cho.

Ví dụ

Phương thức này có chữ ký phương thức sau: (Danh sách chính, gọi lại) (Danh sách chính, hướng, gọi lại)

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

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {

  // Create a collection
  db.createCollection('simple_skip_collection', function(err, collection) {
    assert.equal(null, err);

    // Insert some documents we can sort on
    collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, docs) {
      assert.equal(null, err);

      // Skip one document
      collection.find().skip(1).nextObject(function(err, item) {
        assert.equal(null, err);
        assert.equal(2, item.a);

        db.close();
      });
    });
  });
});

Sắp xếp (Danh sách chính, hướng, gọi lại)

KeyorList (chuỗi) - Đây có thể là một chuỗi hoặc một mảng. Nếu được truyền dưới dạng chuỗi, chuỗi sẽ là trường sắp xếp. Nếu được truyền một mảng, mỗi phần tử sẽ biểu thị một trường được sắp xếp và phải là một mảng chứa định dạng [chuỗi, hướng].

Hướng (chuỗi) - Điều này xác định cách sắp xếp kết quả. & quot; Asc & quot ;, & quot; Ascending & quot; hoặc 1 cho đơn đặt hàng trong khi & quot; desc & quot ;, & quot; giảm dần hoặc -1 cho đơn đặt hàng giảm dần. Lưu ý rằng các chuỗi là trường hợp không nhạy cảm.[, callback]
Arguments:
  • Gọi lại (hàm) - Điều này sẽ được gọi sau khi thực hiện phương thức này. Tham số đầu tiên sẽ chứa một đối tượng lỗi khi con trỏ đã được đóng trong khi tham số thứ hai sẽ chứa tham chiếu đến đối tượng này khi thực hiện thành công. (number) – the new batch size.
  • [Callback] (hàm) - Cuộc gọi lại tùy chọn này sẽ được gọi sau khi thực hiện phương thức này. Tham số đầu tiên sẽ chứa một đối tượng lỗi khi các khoản được đưa ra không phải là số hợp lệ hoặc khi con trỏ đã được đóng trong khi tham số thứ hai sẽ chứa tham chiếu đến đối tượng này khi thực hiện thành công. (function) – this optional callback will be called after executing this method. The first parameter will contain an error object when the batchSize given is not a valid number or when the cursor is already closed while the second parameter will contain a reference to this object upon successful execution.
Returns:

con trỏ một ví dụ của đối tượng này.

Ví dụ

Một ví dụ đơn giản cho thấy việc sử dụng hàng loạt trên con trỏ, việc sử dụng chỉ quy định số lượng tài liệu được trả lại cho mỗi lô

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

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {

  // Create a collection
  db.createCollection('simple_batch_size_collection', function(err, collection) {
    assert.equal(null, err);

    // Insert some documents we can sort on
    collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, docs) {
      assert.equal(null, err);

      // Do normal ascending sort
      collection.find().batchSize(1).nextObject(function(err, item) {
        assert.equal(null, err);
        assert.equal(1, item.a);

        db.close();
      });
    });
  });
});

NextObject¶

Nhận tài liệu tiếp theo từ con trỏ.

NextObject (gọi lại) ¶
Arguments:
  • Gọi lại (hàm) - Điều này sẽ được gọi sau khi thực hiện phương thức này. Tham số đầu tiên sẽ chứa một đối tượng lỗi khi lỗi trong khi tham số thứ hai sẽ chứa một tài liệu từ kết quả được trả về hoặc null nếu không còn kết quả nữa. (function) – this will be called after executing this method. The first parameter will contain an error object on error while the second parameter will contain a document from the returned result or null if there are no more results.

Ví dụ

Một ví dụ đơn giản cho thấy việc sử dụng hàng loạt trên con trỏ, việc sử dụng chỉ quy định số lượng tài liệu được trả lại cho mỗi lô

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

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {

  // Create a collection
  db.createCollection('simple_next_object_collection', function(err, collection) {
    assert.equal(null, err);

    // Insert some documents we can sort on
    collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, docs) {
      assert.equal(null, err);

      // Do normal ascending sort
      collection.find().nextObject(function(err, item) {
        assert.equal(null, err);
        assert.equal(1, item.a);

        db.close();
      });
    });
  });
});

NextObject¶

Nhận tài liệu tiếp theo từ con trỏ.

NextObject (gọi lại) ¶
Arguments:
  • Gọi lại (hàm) - Điều này sẽ được gọi sau khi thực hiện phương thức này. Tham số đầu tiên sẽ chứa một đối tượng lỗi khi lỗi trong khi tham số thứ hai sẽ chứa một tài liệu từ kết quả được trả về hoặc null nếu không còn kết quả nữa. (function) – this will be called after executing this method. The first parameter will always be null while the second parameter will be an object containing the details.

Ví dụ

Một ví dụ đơn giản cho thấy việc sử dụng hàng loạt trên con trỏ, việc sử dụng chỉ quy định số lượng tài liệu được trả lại cho mỗi lô

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

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {

  // Create a collection
  db.createCollection('simple_explain_collection', function(err, collection) {
    assert.equal(null, err);

    // Insert some documents we can sort on
    collection.insert([{a:1}, {a:2}, {a:3}], {w:1}, function(err, docs) {
      assert.equal(null, err);

      // Do normal ascending sort
      collection.find().explain(function(err, explaination) {
        assert.equal(null, err);

        db.close();
      });
    });
  });
});

NextObject¶

Nhận tài liệu tiếp theo từ con trỏ.

NextObject (gọi lại) ¶
  • Gọi lại (hàm) - Điều này sẽ được gọi sau khi thực hiện phương thức này. Tham số đầu tiên sẽ chứa một đối tượng lỗi khi lỗi trong khi tham số thứ hai sẽ chứa một tài liệu từ kết quả được trả về hoặc null nếu không còn kết quả nữa. {Function} function of type function(object) { return transformed }, allows for transformation of data before emitting.
Một ví dụ đơn giản cho thấy việc sử dụng NextObject.
Returns:giải thích¶

Ví dụ

Nhận được một thông tin chi tiết về cách thực hiện truy vấn trên con trỏ này và thời gian mất bao lâu cơ sở dữ liệu để xử lý nó.

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

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {

  // Create a collection to hold our documents
  db.createCollection('test_array', function(err, collection) {

    // Insert a test document
    collection.insert({'b':[1, 2, 3]}, {w:1}, function(err, ids) {

      // Retrieve all the documents in the collection
      collection.find().toArray(function(err, documents) {
        assert.equal(1, documents.length);
        assert.deepEqual([1, 2, 3], documents[0].b);

        db.close();
      });
    });
  });
});
0

Giải thích (gọi lại)

Gọi lại (hàm) - Điều này sẽ được gọi sau khi thực hiện phương thức này. Tham số đầu tiên sẽ luôn là null trong khi tham số thứ hai sẽ là một đối tượng chứa các chi tiết.

Một ví dụ đơn giản cho thấy việc sử dụng chức năng con trỏ giải thích.
Arguments:
  • dòng¶ (function) – this will be called after executing this method. The first parameter will always contain null while the second parameter will contain a reference to this cursor.
Returns:

null

Ví dụ

Trả về giao diện luồng biến đổi nút cho con trỏ này.

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

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {

  // Create a collection to hold our documents
  db.createCollection('test_array', function(err, collection) {

    // Insert a test document
    collection.insert({'b':[1, 2, 3]}, {w:1}, function(err, ids) {

      // Retrieve all the documents in the collection
      collection.find().toArray(function(err, documents) {
        assert.equal(1, documents.length);
        assert.deepEqual([1, 2, 3], documents[0].b);

        db.close();
      });
    });
  });
});
1

Tùy chọn

Transform {function} Hàm của hàm loại (đối tượng) {return biến đổi}, cho phép chuyển đổi dữ liệu trước khi phát ra.

dòng()
Returns:CursorStream trả về một đối tượng luồng.

Ví dụ

Trả về giao diện luồng biến đổi nút cho con trỏ này.

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

var db = new Db('test', new Server('localhost', 27017));
// Establish connection to db
db.open(function(err, db) {

  // Create a collection to hold our documents
  db.createCollection('test_array', function(err, collection) {

    // Insert a test document
    collection.insert({'b':[1, 2, 3]}, {w:1}, function(err, ids) {

      // Retrieve all the documents in the collection
      collection.find().toArray(function(err, documents) {
        assert.equal(1, documents.length);
        assert.deepEqual([1, 2, 3], documents[0].b);

        db.close();
      });
    });
  });
});
2