Làm cách nào để kết nối hai bộ sưu tập trong MongoDB trong nodejs?

Ví dụ về việc sử dụng keepGoing để cho phép hoàn thành chèn hàng loạt ngay cả khi có tài liệu bất hợp pháp trong 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));
  // Only run the rest of the code if we have a mongodb server with version >= 1.9.1
  db.open(function(err, db) {

    // Create a collection
    var collection = db.collection('keepGoingExample');

    // Add an unique index to title to force errors in the batch insert
    collection.ensureIndex({title:1}, {unique:true}, function(err, indexName) {

      // Insert some intial data into the collection
      collection.insert([{name:"Jim"}
        , {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) {

        // Force keep going flag, ignoring unique index issue
        collection.insert([{name:"Jim"}
          , {name:"Sarah", title:"Princess"}
          , {name:'Gump', title:"Gump"}], {w:1, keepGoing:true}, function(err, result) {

          // Count the number of documents left (should not include the duplicates)
          collection.count(function(err, count) {
            assert.equal(3, count);
          })
        });
      });
    });
  });

gỡ bỏ

Removes documents specified by selector from the db.

Tùy chọn
  • w, {Số/Chuỗi, > -1. 'số đông'. tên thẻ} mối quan tâm ghi cho hoạt động trong đó < 1 không xác nhận ghi và w >= 1, w = ‘đa số’ hoặc thẻ xác nhận ghi
  • wtimeout, {Number, 0} đặt thời gian chờ đợi ghi mối quan tâm kết thúc (kết hợp với tùy chọn w)
  • fsync, (Boolean, mặc định. false) ghi đợi fsync trước khi quay lại, từ MongoDB 2. 6 bật, không thể kết hợp fsync với tạp chí
  • j, (Boolean, mặc định. sai) viết chờ đồng bộ nhật ký trước khi quay lại
  • đơn {Boolean, mặc định. false}, xóa tài liệu đầu tiên được tìm thấy
  • fullResult {Boolean, mặc định. false}, trả về tài liệu kết quả đầy đủ (tài liệu được trả về sẽ khác nhau tùy theo phiên bản máy chủ)
xóa ([bộ chọn][, tùy chọn], [gọi lại]) Đối số
  • [bộ chọn] (đối tượng) – chọn tùy chọn, không có bộ chọn nào tương đương với việc xóa tất cả tài liệu
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình xóa
  • [callback] (function) – phải được cung cấp nếu bạn thực hiện thao tác xóa bằng writeconcern
trả lại

vô giá trị

ví dụ

Ví dụ xóa tất cả tài liệu trong bộ sưu tập không sử dụng chế độ an toà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) {

  // Fetch a collection to insert document into
  db.collection("remove_all_documents_no_safe", function(err, collection) {

    // Insert a bunch of documents
    collection.insert([{a:1}, {b:2}], {w:1}, function(err, result) {
      assert.equal(null, err);

      // Remove all the document
      collection.remove();

      // Fetch all results
      collection.find().toArray(function(err, items) {
        assert.equal(null, err);
        assert.equal(0, items.length);
        db.close();
      });
    });
  })
});

Ví dụ xóa một tập hợp con tài liệu bằng chế độ an toàn để đảm bảo xóa tài liệu

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) {

  // Fetch a collection to insert document into
  db.collection("remove_subset_of_documents_safe", function(err, collection) {

    // Insert a bunch of documents
    collection.insert([{a:1}, {b:2}], {w:1}, function(err, result) {
      assert.equal(null, err);

      // Remove all the document
      collection.remove({a:1}, {w:1}, function(err, numberOfRemovedDocs) {
        assert.equal(null, err);
        assert.equal(1, numberOfRemovedDocs);
        db.close();
      });
    });
  })
});

đổi tên

Đổi tên bộ sưu tập

Tùy chọn
  • dropTarget {Boolean, mặc định. false}, bỏ bộ sưu tập tên đích nếu nó tồn tại trước đó
rename(newName [ , tùy chọn ] , gọi lại .
  • newName(string) – tên mới của bộ sưu tập
  • [tùy chọn] (đối tượng) – trả về kết quả tùy chọn
  • gọi lại (chức năng) – gọi lại chấp nhận kết quả
trả lại

vô giá trị

ví dụ

Một ví dụ về đổi tên bất hợp pháp và hợp pháp của một bộ sưu tập

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) {

  // Open a couple of collections
  db.createCollection('test_rename_collection', function(err, collection1) {
    db.createCollection('test_rename_collection2', function(err, collection2) {

      // Attemp to rename a collection to a number
      try {
        collection1.rename(5, function(err, collection) {});
      } catch(err) {
        assert.ok(err instanceof Error);
        assert.equal("collection name must be a String", err.message);
      }

      // Attemp to rename a collection to an empty string
      try {
        collection1.rename("", function(err, collection) {});
      } catch(err) {
        assert.ok(err instanceof Error);
        assert.equal("collection names cannot be empty", err.message);
      }

      // Attemp to rename a collection to an illegal name including the character $
      try {
        collection1.rename("te$t", function(err, collection) {});
      } catch(err) {
        assert.ok(err instanceof Error);
        assert.equal("collection names must not contain '$'", err.message);
      }

      // Attemp to rename a collection to an illegal name starting with the character .
      try {
        collection1.rename(".test", function(err, collection) {});
      } catch(err) {
        assert.ok(err instanceof Error);
        assert.equal("collection names must not start or end with '.'", err.message);
      }

      // Attemp to rename a collection to an illegal name ending with the character .
      try {
        collection1.rename("test.", function(err, collection) {});
      } catch(err) {
        assert.ok(err instanceof Error);
        assert.equal("collection names must not start or end with '.'", err.message);
      }

      // Attemp to rename a collection to an illegal name with an empty middle name
      try {
        collection1.rename("tes..t", function(err, collection) {});
      } catch(err) {
        assert.equal("collection names cannot be empty", err.message);
      }

      // Insert a couple of documents
      collection1.insert([{'x':1}, {'x':2}], {w:1}, function(err, docs) {

        // Attemp to rename the first collection to the second one, this will fail
        collection1.rename('test_rename_collection2', function(err, collection) {
          assert.ok(err instanceof Error);
          assert.ok(err.message.length > 0);

          // Attemp to rename the first collection to a name that does not exist
          // this will be succesful
          collection1.rename('test_rename_collection3', function(err, collection) {
            assert.equal("test_rename_collection3", collection.collectionName);

            // Ensure that the collection is pointing to the new one
            collection1.count(function(err, count) {
              assert.equal(2, count);
              db.close();
            });
          });
        });
      })
    });
  });
});

tiết kiệm

Lưu tài liệu. Chức năng thay thế tài liệu đầy đủ đơn giản. Không được đề xuất cho hiệu quả, thay vào đó hãy sử dụng toán tử nguyên tử và cập nhật để hoạt động hiệu quả hơn

Tùy chọn
  • w, {Số/Chuỗi, > -1. 'số đông'. tên thẻ} mối quan tâm ghi cho hoạt động trong đó < 1 không xác nhận ghi và w >= 1, w = ‘đa số’ hoặc thẻ xác nhận ghi
  • wtimeout, {Number, 0} đặt thời gian chờ đợi ghi mối quan tâm kết thúc (kết hợp với tùy chọn w)
  • fsync, (Boolean, mặc định. false) ghi đợi fsync trước khi quay lại, từ MongoDB 2. 6 bật, không thể kết hợp fsync với tạp chí
  • j, (Boolean, mặc định. sai) viết chờ đồng bộ nhật ký trước khi quay lại
lưu([doc][, tùy chọn], [gọi lại])Đối số
  • [doc] (đối tượng) – tài liệu cần lưu
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình xóa
  • [gọi lại] (chức năng) – phải được cung cấp nếu bạn thực hiện cập nhật với mối quan tâm ghi
trả lại

vô giá trị

ví dụ

Ví dụ về lưu tài liệu đơn giản với cài đặt an toàn thành false

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) {

  // Fetch the collection
  var collection = db.collection("save_a_simple_document");
  // Save a document with no safe option
  collection.save({hello:'world'});

  // Wait for a second
  setTimeout(function() {

    // Find the saved document
    collection.findOne({hello:'world'}, function(err, item) {
      assert.equal(null, err);
      assert.equal('world', item.hello);
      db.close();
    });
  }, 1000);
});

Ví dụ về một tài liệu đơn giản lưu rồi lưu lại với safe được đặt thành true

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) {

  // Fetch the collection
  var collection = db.collection("save_a_simple_document_modify_it_and_resave_it");

  // Save a document with no safe option
  collection.save({hello:'world'}, {w: 0}, function(err, result) {

    // Find the saved document
    collection.findOne({hello:'world'}, function(err, item) {
      assert.equal(null, err);
      assert.equal('world', item.hello);

      // Update the document
      item['hello2'] = 'world2';

      // Save the item with the additional field
      collection.save(item, {w: 1}, function(err, result) {

        // Find the changed document
        collection.findOne({hello:'world'}, function(err, item) {
          assert.equal(null, err);
          assert.equal('world', item.hello);
          assert.equal('world2', item.hello2);

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

cập nhật

Cập nhật tài liệu

Tùy chọn
  • w, {Số/Chuỗi, > -1. 'số đông'. tên thẻ} mối quan tâm ghi cho hoạt động trong đó < 1 không xác nhận ghi và w >= 1, w = ‘đa số’ hoặc thẻ xác nhận ghi
  • wtimeout, {Number, 0} đặt thời gian chờ đợi ghi mối quan tâm kết thúc (kết hợp với tùy chọn w)
  • fsync, (Boolean, mặc định. false) ghi đợi fsync trước khi quay lại, từ MongoDB 2. 6 bật, không thể kết hợp fsync với tạp chí
  • j, (Boolean, mặc định. sai) viết chờ đồng bộ nhật ký trước khi quay lại
  • upsert {Boolean, mặc định. false}, thực hiện thao tác upsert
  • đa {Boolean, mặc định. false}, cập nhật tất cả tài liệu khớp với bộ chọn
  • serializeFunctions {Boolean, mặc định. false}, tuần tự hóa các chức năng trên tài liệu
  • checkKeys {Boolean, mặc định. true}, cho phép tắt tính năng kiểm tra khóa tài liệu (CẢNH BÁO MỞ CHO BẠN LÊN CÁC TẤN CÔNG TIÊM)
  • fullResult {Boolean, mặc định. false}, trả về tài liệu kết quả đầy đủ (tài liệu được trả về sẽ khác nhau tùy theo phiên bản máy chủ)
update(selector, document [ , options ] . , callback])Arguments:
  • bộ chọn (đối tượng) – truy vấn để chọn tài liệu/tài liệu sẽ được cập nhật
  • tài liệu (đối tượng) – các trường/val sẽ được cập nhật hoặc trong trường hợp thao tác upsert, được chèn vào
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình cập nhật
  • [gọi lại] (chức năng) – phải được cung cấp nếu bạn thực hiện cập nhật với mối quan tâm ghi
trả lại

vô giá trị

ví dụ

Ví dụ về một bản cập nhật tài liệu đơn giản với an toàn được đặt thành false trên một tài liệu hiện có

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) {

  // Get a collection
  db.collection('update_a_simple_document', function(err, collection) {

    // Insert a document, then update it
    collection.insert({a:1}, {w: 1}, function(err, doc) {

      // Update the document with an atomic operator
      collection.update({a:1}, {$set:{b:2}});

      // Wait for a second then fetch the document
      setTimeout(function() {

        // Fetch the document that we modified
        collection.findOne({a:1}, function(err, item) {
          assert.equal(null, err);
          assert.equal(1, item.a);
          assert.equal(2, item.b);
          db.close();
        });
      }, 1000);
    })
  });
});

Ví dụ về cập nhật tài liệu đơn giản bằng cách sử dụng upert (tài liệu sẽ được chèn vào nếu nó không tồn tạ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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("batch_document_insert_collection_safe");
  // Insert a single document
  collection.insert([{hello:'world_safe1'}
    , {hello:'world_safe2'}], {w:1}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world_safe2'}, function(err, item) {
      assert.equal(null, err);
      assert.equal('world_safe2', item.hello);
      db.close();
    })
  });
});
0

Ví dụ về cập nhật trên nhiều tài liệu bằng cách sử dụng nhiều tùy chọ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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("batch_document_insert_collection_safe");
  // Insert a single document
  collection.insert([{hello:'world_safe1'}
    , {hello:'world_safe2'}], {w:1}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world_safe2'}, function(err, item) {
      assert.equal(null, err);
      assert.equal('world_safe2', item.hello);
      db.close();
    })
  });
});
1

riêng biệt

Lệnh riêng biệt trả về trả về một danh sách các giá trị riêng biệt cho khóa đã cho trên một bộ sưu tập

Tùy chọn
  • readPreference {String}, tùy chọn đọc ưu tiên, yêu cầu ('mongodb'). Tùy chọn đọc (ReadPreference. CHÍNH, ReadPreference. PRIMARY_PREFERRED, ReadPreference. TRUNG HỌC, ReadPreference. SECONDARY_PREFERRED, ReadPreference. GẦN NHẤT)
distinct(key [ , truy vấn ][< . , options], callback)Arguments:
  • key (string) – key để chạy khác biệt với
  • [truy vấn] (đối tượng) – truy vấn tùy chọn để thu hẹp các đối tượng được trả về
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình cập nhật
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ khác biệt hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Ví dụ về chạy lệnh riêng biệt đối với bộ sưu tập

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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("batch_document_insert_collection_safe");
  // Insert a single document
  collection.insert([{hello:'world_safe1'}
    , {hello:'world_safe2'}], {w:1}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world_safe2'}, function(err, item) {
      assert.equal(null, err);
      assert.equal('world_safe2', item.hello);
      db.close();
    })
  });
});
2

Ví dụ về chạy lệnh riêng biệt đối với bộ sưu tập có truy vấn bộ lọc

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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("batch_document_insert_collection_safe");
  // Insert a single document
  collection.insert([{hello:'world_safe1'}
    , {hello:'world_safe2'}], {w:1}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world_safe2'}, function(err, item) {
      assert.equal(null, err);
      assert.equal('world_safe2', item.hello);
      db.close();
    })
  });
});
3

đếm

Đếm số lượng tài liệu phù hợp trong db cho một truy vấn

Tùy chọn
  • bỏ qua {Number}, Số lượng tài liệu cần bỏ qua để đếm
  • giới hạn {Number}, Giới hạn tài liệu để đếm
  • readPreference {String}, tùy chọn đọc ưu tiên, yêu cầu ('mongodb'). Tùy chọn đọc (ReadPreference. CHÍNH, ReadPreference. PRIMARY_PREFERRED, ReadPreference. TRUNG HỌC, ReadPreference. SECONDARY_PREFERRED, ReadPreference. GẦN NHẤT)
đếm([truy vấn][, tùy chọn], gọi lại)Đối số
  • [truy vấn] (đối tượng) – truy vấn để lọc theo trước khi thực hiện đếm
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình đếm
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức đếm hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Ví dụ về việc chạy các lệnh đếm đơn giản đối với một bộ sưu tập

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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("batch_document_insert_collection_safe");
  // Insert a single document
  collection.insert([{hello:'world_safe1'}
    , {hello:'world_safe2'}], {w:1}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world_safe2'}, function(err, item) {
      assert.equal(null, err);
      assert.equal('world_safe2', item.hello);
      db.close();
    })
  });
});
4

làm rơi

Thả bộ sưu tập

thả (gọi lại) Đối số
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức drop hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Ví dụ về một tài liệu đơn giản lưu rồi lưu lại với safe được đặt thành true

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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("batch_document_insert_collection_safe");
  // Insert a single document
  collection.insert([{hello:'world_safe1'}
    , {hello:'world_safe2'}], {w:1}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world_safe2'}, function(err, item) {
      assert.equal(null, err);
      assert.equal('world_safe2', item.hello);
      db.close();
    })
  });
});
5

findAndModify

Tìm và cập nhật tài liệu

Tùy chọn
  • w, {Số/Chuỗi, > -1. 'số đông'. tên thẻ} mối quan tâm ghi cho hoạt động trong đó < 1 không xác nhận ghi và w >= 1, w = ‘đa số’ hoặc thẻ xác nhận ghi
  • wtimeout, {Number, 0} đặt thời gian chờ đợi ghi mối quan tâm kết thúc (kết hợp với tùy chọn w)
  • fsync, (Boolean, mặc định. false) ghi đợi fsync trước khi quay lại, từ MongoDB 2. 6 bật, không thể kết hợp fsync với tạp chí
  • j, (Boolean, mặc định. sai) viết chờ đồng bộ nhật ký trước khi quay lại
  • xóa {Boolean, mặc định. false}, đặt thành true để xóa đối tượng trước khi quay lại
  • upsert {Boolean, mặc định. false}, thực hiện thao tác upsert
  • mới {Boolean, mặc định. false}, đặt thành true nếu bạn muốn trả lại đối tượng đã sửa đổi thay vì đối tượng ban đầu. Bỏ qua để loại bỏ
findAndModify(query, sort, doc [ , options ], callback)Arguments:
  • truy vấn (đối tượng) – đối tượng truy vấn để định vị đối tượng cần sửa đổi
  • sắp xếp (mảng) –
    • nếu nhiều tài liệu khớp nhau, hãy chọn tài liệu đầu tiên theo thứ tự sắp xếp đã chỉ định làm đối tượng để thao tác
  • tài liệu (đối tượng) –
    • các trường/vals sẽ được cập nhật
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình cập nhật
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức findAndModify hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Toàn bộ các cách khác nhau để sử dụng lệnh findAndModify

Lệnh findAndModify đầu tiên sửa đổi tài liệu và trả lại tài liệu đã sửa đổi. Lệnh findAndModify thứ hai xóa tài liệu. Lệnh findAndModify thứ hai nâng cấp tài liệu và trả về tài liệu mớ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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("batch_document_insert_collection_safe");
  // Insert a single document
  collection.insert([{hello:'world_safe1'}
    , {hello:'world_safe2'}], {w:1}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world_safe2'}, function(err, item) {
      assert.equal(null, err);
      assert.equal('world_safe2', item.hello);
      db.close();
    })
  });
});
6

tìmAndRemove

Tìm và xóa tài liệu

Tùy chọn
  • w, {Số/Chuỗi, > -1. 'số đông'. tên thẻ} mối quan tâm ghi cho hoạt động trong đó < 1 không xác nhận ghi và w >= 1, w = ‘đa số’ hoặc thẻ xác nhận ghi
  • wtimeout, {Number, 0} đặt thời gian chờ đợi ghi mối quan tâm kết thúc (kết hợp với tùy chọn w)
  • fsync, (Boolean, mặc định. false) ghi đợi fsync trước khi quay lại, từ MongoDB 2. 6 bật, không thể kết hợp fsync với tạp chí
  • j, (Boolean, mặc định. sai) viết chờ đồng bộ nhật ký trước khi quay lại
findAndRemove(truy vấn, sắp xếp [ , tùy chọn ] .
  • truy vấn (đối tượng) – đối tượng truy vấn để định vị đối tượng cần sửa đổi
  • sắp xếp (mảng) –
    • nếu nhiều tài liệu khớp nhau, hãy chọn tài liệu đầu tiên theo thứ tự sắp xếp đã chỉ định làm đối tượng để thao tác
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình cập nhật
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức findAndRemove hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Một ví dụ về việc sử dụng findAndRemove

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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("batch_document_insert_collection_safe");
  // Insert a single document
  collection.insert([{hello:'world_safe1'}
    , {hello:'world_safe2'}], {w:1}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world_safe2'}, function(err, item) {
      assert.equal(null, err);
      assert.equal('world_safe2', item.hello);
      db.close();
    })
  });
});
7

tìm thấy

Tạo một con trỏ cho một truy vấn có thể được sử dụng để lặp lại các kết quả từ MongoDB

khả năng lập luận khác nhau
  • gọi lại?
  • bộ chọn, gọi lại?,
  • bộ chọn, trường, gọi lại?
  • bộ chọn, tùy chọn, gọi lại?
  • bộ chọn, trường, tùy chọn, gọi lại?
  • bộ chọn, trường, bỏ qua, giới hạn, gọi lại?
  • bộ chọn, trường, bỏ qua, giới hạn, thời gian chờ, gọi lại?
Tùy chọn
  • giới hạn {Số, mặc định. 0}, đặt giới hạn tài liệu được trả về trong truy vấn
  • sắp xếp {Mảng. Object}, được đặt để sắp xếp các tài liệu quay lại từ truy vấn. Mảng chỉ mục, [[‘a’, 1]], v.v.
  • các trường {Object}, các trường sẽ trả về trong truy vấn. Đối tượng của các trường để bao gồm hoặc loại trừ (không phải cả hai), {‘a’. 1}
  • bỏ qua {Số, mặc định. 0}, được đặt để bỏ qua N tài liệu phía trước trong truy vấn của bạn (hữu ích cho việc phân trang)
  • gợi ý {Object}, yêu cầu truy vấn sử dụng các chỉ mục cụ thể trong truy vấn. Đối tượng của các chỉ mục sẽ sử dụng, {‘_id’. 1}
  • giải thích {Boolean, mặc định. false}, giải thích truy vấn thay vì trả về dữ liệu
  • ảnh chụp nhanh {Boolean, mặc định. false}, truy vấn ảnh chụp nhanh
  • thời gian chờ {Boolean, mặc định. false}, chỉ định nếu con trỏ có thể hết thời gian chờ
  • tailable {Boolean, mặc định. false}, chỉ định xem con trỏ có thể điều chỉnh được không
  • tailableRetryInterval {Số, mặc định. 100}, chỉ định mili giây giữa getMores trên con trỏ có thể điều chỉnh
  • numberOfRetries {Số, mặc định. 5}, chỉ định số lần thử lại con trỏ có thể theo đuôi
  • awaitdata {Boolean, mặc định. false} cho phép con trỏ chờ dữ liệu, chỉ áp dụng cho con trỏ có đuôi
  • oplogReplay {Boolean, mặc định. false} đặt cờ nội bộ, chỉ áp dụng cho con trỏ có thể theo đuôi
  • xả {Boolean, mặc định. false} yêu cầu máy chủ gửi tất cả tài liệu cùng một lúc dưới dạng gói getMore, không được khuyến nghị
  • batchSize {Số, mặc định. 0}, hãy đặt kích thước lô cho getMoreCommand khi lặp lại kết quả truy vấn
  • returnKey {Boolean, mặc định. false}, chỉ trả lại khóa chỉ mục
  • maxScan {Number}, Giới hạn số mục cần quét
  • tối thiểu {Number}, Đặt giới hạn chỉ mục
  • tối đa {Number}, Đặt giới hạn chỉ mục
  • showDiskLoc {Boolean, mặc định. false}, Hiển thị vị trí đĩa của kết quả
  • nhận xét {Chuỗi}, Bạn có thể đặt trường nhận xét $ vào truy vấn để giúp việc tìm kiếm trong nhật ký hồ sơ đơn giản hơn
  • thô {Boolean, mặc định. false}, Trả lại tất cả tài liệu BSON dưới dạng tài liệu Bộ đệm thô
  • readPreference {String}, tùy chọn đọc ưu tiên, yêu cầu ('mongodb'). ReadPreference ((ReadPreference. CHÍNH, ReadPreference. PRIMARY_PREFERRED, ReadPreference. TRUNG HỌC, ReadPreference. SECONDARY_PREFERRED, ReadPreference. GẦN NHẤT)
  • numberOfRetries {Số, mặc định. 5}, nếu sử dụng awaidata chỉ định số lần thử lại khi hết thời gian chờ
  • một phần {Boolean, mặc định. false}, chỉ định xem con trỏ có trả về một phần kết quả khi truy vấn hệ thống được phân mảnh hay không
  • maxTimeMS {Number}, số mili giây phải đợi trước khi hủy bỏ truy vấn
find(truy vấn [ , tùy chọn ] , gọi lại .
  • truy vấn (đối tượng) – đối tượng truy vấn để định vị đối tượng cần sửa đổi
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình cập nhật
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức tìm hoặc null nếu xảy ra lỗi
trả lại

con trỏ trả về một con trỏ cho truy vấn

ví dụ

Một truy vấn đơn giản sử dụng phương thức tìm kiếm trên bộ sưu tập

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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("batch_document_insert_collection_safe");
  // Insert a single document
  collection.insert([{hello:'world_safe1'}
    , {hello:'world_safe2'}], {w:1}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world_safe2'}, function(err, item) {
      assert.equal(null, err);
      assert.equal('world_safe2', item.hello);
      db.close();
    })
  });
});
8

Một truy vấn đơn giản hiển thị giải thích cho một truy vấ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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("batch_document_insert_collection_safe");
  // Insert a single document
  collection.insert([{hello:'world_safe1'}
    , {hello:'world_safe2'}], {w:1}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world_safe2'}, function(err, item) {
      assert.equal(null, err);
      assert.equal('world_safe2', item.hello);
      db.close();
    })
  });
});
9

Một truy vấn đơn giản hiển thị bỏ qua và giới hạ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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("simple_document_insert_with_function_safe");
  // Insert a single document
  collection.insert({hello:'world'
    , func:function() {}}, {w:1, serializeFunctions:true}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world'}, function(err, item) {
      assert.equal(null, err);
      assert.ok("function() {}", item.code);
      db.close();
    })
  });
});
0

tìm một

Tìm một tài liệu duy nhất dựa trên truy vấn

khả năng lập luận khác nhau
  • gọi lại?
  • bộ chọn, gọi lại?,
  • bộ chọn, trường, gọi lại?
  • bộ chọn, tùy chọn, gọi lại?
  • bộ chọn, trường, tùy chọn, gọi lại?
  • bộ chọn, trường, bỏ qua, giới hạn, gọi lại?
  • bộ chọn, trường, bỏ qua, giới hạn, thời gian chờ, gọi lại?
Tùy chọn
  • giới hạn {Số, mặc định. 0}, đặt giới hạn tài liệu được trả về trong truy vấn
  • sắp xếp {Mảng. Object}, được đặt để sắp xếp các tài liệu quay lại từ truy vấn. Mảng chỉ mục, [[‘a’, 1]], v.v.
  • các trường {Object}, các trường sẽ trả về trong truy vấn. Đối tượng của các trường để bao gồm hoặc loại trừ (không phải cả hai), {‘a’. 1}
  • bỏ qua {Số, mặc định. 0}, được đặt để bỏ qua N tài liệu phía trước trong truy vấn của bạn (hữu ích cho việc phân trang)
  • gợi ý {Object}, yêu cầu truy vấn sử dụng các chỉ mục cụ thể trong truy vấn. Đối tượng của các chỉ mục sẽ sử dụng, {‘_id’. 1}
  • giải thích {Boolean, mặc định. false}, giải thích truy vấn thay vì trả về dữ liệu
  • ảnh chụp nhanh {Boolean, mặc định. false}, truy vấn ảnh chụp nhanh
  • thời gian chờ {Boolean, mặc định. false}, chỉ định nếu con trỏ có thể hết thời gian chờ
  • tailable {Boolean, mặc định. false}, chỉ định xem con trỏ có thể điều chỉnh được không
  • batchSize {Số, mặc định. 0}, hãy đặt kích thước lô cho getMoreCommand khi lặp lại kết quả truy vấn
  • returnKey {Boolean, mặc định. false}, chỉ trả lại khóa chỉ mục
  • maxScan {Number}, Giới hạn số mục cần quét
  • tối thiểu {Number}, Đặt giới hạn chỉ mục
  • tối đa {Number}, Đặt giới hạn chỉ mục
  • showDiskLoc {Boolean, mặc định. false}, Hiển thị vị trí đĩa của kết quả
  • nhận xét {Chuỗi}, Bạn có thể đặt trường nhận xét $ vào truy vấn để giúp việc tìm kiếm trong nhật ký hồ sơ đơn giản hơn
  • thô {Boolean, mặc định. false}, Trả lại tất cả tài liệu BSON dưới dạng tài liệu Bộ đệm thô
  • readPreference {String}, tùy chọn đọc ưu tiên, yêu cầu ('mongodb'). Tùy chọn đọc (ReadPreference. CHÍNH, ReadPreference. PRIMARY_PREFERRED, ReadPreference. TRUNG HỌC, ReadPreference. SECONDARY_PREFERRED, ReadPreference. GẦN NHẤT)
  • một phần {Boolean, mặc định. false}, chỉ định xem con trỏ có trả về một phần kết quả khi truy vấn hệ thống được phân mảnh hay không
  • maxTimeMS {Number}, số mili giây phải đợi trước khi hủy bỏ truy vấn
findOne(truy vấn [ , tùy chọn ] , gọi lại .
  • truy vấn (đối tượng) – đối tượng truy vấn để định vị đối tượng cần sửa đổi
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình cập nhật
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức findOne hoặc null nếu xảy ra lỗi
trả lại

con trỏ trả về một con trỏ cho truy vấn

ví dụ

Một truy vấn đơn giản sử dụng findOne

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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("simple_document_insert_with_function_safe");
  // Insert a single document
  collection.insert({hello:'world'
    , func:function() {}}, {w:1, serializeFunctions:true}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world'}, function(err, item) {
      assert.equal(null, err);
      assert.ok("function() {}", item.code);
      db.close();
    })
  });
});
1

tạo Index

Tạo một chỉ mục trên bộ sưu tập

Tùy chọn
  • w, {Số/Chuỗi, > -1. 'số đông'. tên thẻ} mối quan tâm ghi cho hoạt động trong đó < 1 không xác nhận ghi và w >= 1, w = ‘đa số’ hoặc thẻ xác nhận ghi
  • wtimeout, {Number, 0} đặt thời gian chờ đợi ghi mối quan tâm kết thúc (kết hợp với tùy chọn w)
  • fsync, (Boolean, mặc định. false) ghi đợi fsync trước khi quay lại, từ MongoDB 2. 6 bật, không thể kết hợp fsync với tạp chí
  • j, (Boolean, mặc định. sai) viết chờ đồng bộ nhật ký trước khi quay lại
  • duy nhất {Boolean, mặc định. false}, tạo một chỉ mục duy nhất
  • thưa thớt {Boolean, mặc định. false}, tạo một chỉ mục thưa thớt
  • nền {Boolean, mặc định. false}, tạo chỉ mục trong nền, mang lại bất cứ khi nào có thể
  • dropDups {Boolean, mặc định. false}, không thể tạo một chỉ mục duy nhất trên khóa có các giá trị trùng lặp từ trước. Nếu bạn vẫn muốn tạo chỉ mục, hãy giữ tài liệu đầu tiên làm chỉ mục cơ sở dữ liệu và xóa tất cả các tài liệu tiếp theo có giá trị trùng lặp
  • tối thiểu {Number}, đối với các chỉ mục không gian địa lý, hãy đặt giới hạn dưới cho các tọa độ
  • tối đa {Number}, đối với các chỉ mục không gian địa lý, hãy đặt giới hạn cao cho các tọa độ
  • v {Number}, chỉ định phiên bản định dạng của các chỉ mục
  • expireafterSeconds {Number}, cho phép bạn hết hạn dữ liệu trên các chỉ mục được áp dụng cho dữ liệu (MongoDB 2. 2 hoặc cao hơn)
  • tên {String}, ghi đè tên chỉ mục được tạo tự động (hữu ích nếu tên kết quả lớn hơn 128 byte)
createIndex(fieldOrSpec [ , tùy chọn ] , gọi lại .
  • fieldOrSpec (đối tượng) – fieldOrSpec xác định chỉ mục
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình cập nhật
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức createIndex hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Tạo một chỉ mục đơn giản bằng cách sử dụng một chỉ mục trường đơn giả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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("simple_document_insert_with_function_safe");
  // Insert a single document
  collection.insert({hello:'world'
    , func:function() {}}, {w:1, serializeFunctions:true}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world'}, function(err, item) {
      assert.equal(null, err);
      assert.ok("function() {}", item.code);
      db.close();
    })
  });
});
2

Tạo Index phức tạp hơn bằng cách sử dụng chỉ mục tổng hợp duy nhất trong nền và loại bỏ các tài liệu trùng lặp

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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("simple_document_insert_with_function_safe");
  // Insert a single document
  collection.insert({hello:'world'
    , func:function() {}}, {w:1, serializeFunctions:true}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world'}, function(err, item) {
      assert.equal(null, err);
      assert.ok("function() {}", item.code);
      db.close();
    })
  });
});
3

đảm bảo Index

Đảm bảo rằng một chỉ mục tồn tại, nếu không, nó sẽ tạo ra nó

Tùy chọn
  • w, {Số/Chuỗi, > -1. 'số đông'. tên thẻ} mối quan tâm ghi cho hoạt động trong đó < 1 không xác nhận ghi và w >= 1, w = ‘đa số’ hoặc thẻ xác nhận ghi
  • wtimeout, {Number, 0} đặt thời gian chờ đợi ghi mối quan tâm kết thúc (kết hợp với tùy chọn w)
  • fsync, (Boolean, mặc định. false) ghi đợi fsync trước khi quay lại, từ MongoDB 2. 6 bật, không thể kết hợp fsync với tạp chí
  • j, (Boolean, mặc định. sai) viết chờ đồng bộ nhật ký trước khi quay lại
  • duy nhất {Boolean, mặc định. false}, tạo một chỉ mục duy nhất
  • thưa thớt {Boolean, mặc định. false}, tạo một chỉ mục thưa thớt
  • nền {Boolean, mặc định. false}, tạo chỉ mục trong nền, mang lại bất cứ khi nào có thể
  • dropDups {Boolean, mặc định. false}, không thể tạo một chỉ mục duy nhất trên khóa có các giá trị trùng lặp từ trước. Nếu bạn vẫn muốn tạo chỉ mục, hãy giữ tài liệu đầu tiên làm chỉ mục cơ sở dữ liệu và xóa tất cả các tài liệu tiếp theo có giá trị trùng lặp
  • tối thiểu {Number}, đối với các chỉ mục không gian địa lý, hãy đặt giới hạn dưới cho các tọa độ
  • tối đa {Number}, đối với các chỉ mục không gian địa lý, hãy đặt giới hạn cao cho các tọa độ
  • v {Number}, chỉ định phiên bản định dạng của các chỉ mục
  • expireafterSeconds {Number}, cho phép bạn hết hạn dữ liệu trên các chỉ mục được áp dụng cho dữ liệu (MongoDB 2. 2 hoặc cao hơn)
  • tên {String}, ghi đè tên chỉ mục được tạo tự động (hữu ích nếu tên kết quả lớn hơn 128 byte)
ensureIndex(fieldOrSpec [ , tùy chọn ] , gọi lại .
  • fieldOrSpec (đối tượng) – fieldOrSpec xác định chỉ mục
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình cập nhật
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức ensureIndex hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Một ensureIndex phức tạp hơn bằng cách sử dụng một chỉ mục tổng hợp duy nhất trong nền và loại bỏ các tài liệu trùng lặp

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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("simple_document_insert_with_function_safe");
  // Insert a single document
  collection.insert({hello:'world'
    , func:function() {}}, {w:1, serializeFunctions:true}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world'}, function(err, item) {
      assert.equal(null, err);
      assert.ok("function() {}", item.code);
      db.close();
    })
  });
});
4

mục lụcThông tin

Truy xuất thông tin chỉ mục bộ sưu tập này

Tùy chọn
  • đầy đủ {Boolean, mặc định. false}, trả về thông tin chỉ mục thô đầy đủ
indexInformation( [ options ] , callback)Arguments.
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình cập nhật
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức indexInformation hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("simple_document_insert_with_function_safe");
  // Insert a single document
  collection.insert({hello:'world'
    , func:function() {}}, {w:1, serializeFunctions:true}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world'}, function(err, item) {
      assert.equal(null, err);
      assert.ok("function() {}", item.code);
      db.close();
    })
  });
});
5

thả Index

Loại bỏ một chỉ mục từ bộ sưu tập này

dropIndex(tên, gọi lại) Đối số
  • tên (chuỗi) –
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức dropIndex hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Một ví dụ cho thấy việc tạo và loại bỏ một chỉ mục

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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("simple_document_insert_with_function_safe");
  // Insert a single document
  collection.insert({hello:'world'
    , func:function() {}}, {w:1, serializeFunctions:true}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world'}, function(err, item) {
      assert.equal(null, err);
      assert.ok("function() {}", item.code);
      db.close();
    })
  });
});
6

dropAll Indexes

Bỏ tất cả các chỉ mục từ bộ sưu tập này

dropAllIndexes(callback)Đối số
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức dropAllIndexes hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

chỉ mục lại

Lập chỉ mục lại tất cả các chỉ mục trên bộ sưu tập Cảnh báo. reIndex là một hoạt động chặn (các chỉ mục được xây dựng lại ở phía trước) và sẽ chậm đối với các bộ sưu tập lớn

lập chỉ mục lại (gọi lại) Đối số
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức reIndex hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Một ví dụ cho thấy cách buộc lập chỉ mục lại bộ sưu tập

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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("simple_document_insert_with_function_safe");
  // Insert a single document
  collection.insert({hello:'world'
    , func:function() {}}, {w:1, serializeFunctions:true}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world'}, function(err, item) {
      assert.equal(null, err);
      assert.ok("function() {}", item.code);
      db.close();
    })
  });
});
7

bản đồGiảm

Chạy Map Giảm trên một bộ sưu tập. Xin lưu ý rằng tùy chọn nội tuyến cho out sẽ trả về một mảng kết quả chứ không phải tập hợp

Tùy chọn
  • out {Object}, đặt mục tiêu đầu ra cho công việc thu nhỏ bản đồ. {nội tuyến. 1}. {thay thế. 'tên bộ sưu tập'}. {hợp nhất. 'tên bộ sưu tập'}. {giảm. 'tên bộ sưu tập'}
  • truy vấn {Object}, đối tượng bộ lọc truy vấn
  • sắp xếp {Object}, sắp xếp các đối tượng đầu vào bằng phím này. Hữu ích cho việc tối ưu hóa, như sắp xếp theo phím phát ra để giảm ít hơn
  • giới hạn {Number}, số đối tượng trả về từ bộ sưu tập
  • keeptemp {Boolean, mặc định. false}, giữ dữ liệu tạm thời
  • hoàn thiện {Chức năng. String}, hoàn thiện chức năng
  • phạm vi {Object}, có thể chuyển các biến có thể truy cập từ bản đồ/thu nhỏ/hoàn thiện
  • jsMode {Boolean, mặc định. false}, có thể giữ nguyên việc thực thi trong JS. Được cung cấp trong MongoDB > 2. 0. X
  • dài dòng {Boolean, mặc định. false}, cung cấp số liệu thống kê về thời gian thực hiện công việc
  • readPreference {Chuỗi, chỉ dành cho kết quả nội tuyến}, tùy chọn đọc ưu tiên, yêu cầu ('mongodb'). Tùy chọn đọc (ReadPreference. CHÍNH, ReadPreference. PRIMARY_PREFERRED, ReadPreference. TRUNG HỌC, ReadPreference. SECONDARY_PREFERRED, ReadPreference. GẦN NHẤT)
mapReduce(map, reduce [ , tùy chọn ] .
  • map (function) – chức năng lập bản đồ
  • giảm (chức năng) – chức năng giảm
  • [options] (đối tượng) – tùy chọn cho bản đồ giảm công việc
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức mapReduce hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Một ví dụ rút gọn bản đồ đơn giả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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("simple_document_insert_with_function_safe");
  // Insert a single document
  collection.insert({hello:'world'
    , func:function() {}}, {w:1, serializeFunctions:true}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world'}, function(err, item) {
      assert.equal(null, err);
      assert.ok("function() {}", item.code);
      db.close();
    })
  });
});
8

Một ví dụ rút gọn bản đồ đơn giản bằng cách sử dụng loại đầu ra nội tuyến trên MongoDB > 1. 7. 6 trả lại số liệu thống kê

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));
db.open(function(err, db) {
  // Fetch a collection to insert document into
  var collection = db.collection("simple_document_insert_with_function_safe");
  // Insert a single document
  collection.insert({hello:'world'
    , func:function() {}}, {w:1, serializeFunctions:true}, function(err, result) {
    assert.equal(null, err);

    // Fetch the document
    collection.findOne({hello:'world'}, function(err, item) {
      assert.equal(null, err);
      assert.ok("function() {}", item.code);
      db.close();
    })
  });
});
9

Mapreduce thử nghiệm khác nhau với phạm vi được cung cấp có chứa chức năng javascript

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));
  // Only run the rest of the code if we have a mongodb server with version >= 1.9.1
  db.open(function(err, db) {

    // Create a collection
    var collection = db.collection('keepGoingExample');

    // Add an unique index to title to force errors in the batch insert
    collection.ensureIndex({title:1}, {unique:true}, function(err, indexName) {

      // Insert some intial data into the collection
      collection.insert([{name:"Jim"}
        , {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) {

        // Force keep going flag, ignoring unique index issue
        collection.insert([{name:"Jim"}
          , {name:"Sarah", title:"Princess"}
          , {name:'Gump', title:"Gump"}], {w:1, keepGoing:true}, function(err, result) {

          // Count the number of documents left (should not include the duplicates)
          collection.count(function(err, count) {
            assert.equal(3, count);
          })
        });
      });
    });
  });
0

Mapreduce thử nghiệm khác nhau với phạm vi được cung cấp có chứa các đối tượng javascript có chức năng

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));
  // Only run the rest of the code if we have a mongodb server with version >= 1.9.1
  db.open(function(err, db) {

    // Create a collection
    var collection = db.collection('keepGoingExample');

    // Add an unique index to title to force errors in the batch insert
    collection.ensureIndex({title:1}, {unique:true}, function(err, indexName) {

      // Insert some intial data into the collection
      collection.insert([{name:"Jim"}
        , {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) {

        // Force keep going flag, ignoring unique index issue
        collection.insert([{name:"Jim"}
          , {name:"Sarah", title:"Princess"}
          , {name:'Gump', title:"Gump"}], {w:1, keepGoing:true}, function(err, result) {

          // Count the number of documents left (should not include the duplicates)
          collection.count(function(err, count) {
            assert.equal(3, count);
          })
        });
      });
    });
  });
1

nhóm

Chạy một lệnh nhóm trên một bộ sưu tập

Tùy chọn
  • readPreference {String}, tùy chọn đọc ưu tiên, yêu cầu ('mongodb'). Tùy chọn đọc (ReadPreference. CHÍNH, ReadPreference. PRIMARY_PREFERRED, ReadPreference. TRUNG HỌC, ReadPreference. SECONDARY_PREFERRED, ReadPreference. GẦN NHẤT)
nhóm(phím, điều kiện, ban đầu, rút ​​gọn, hoàn thiện, lệnh [ , tùy chọn ]< . , callback)Arguments:
  • keys (object) – một đối tượng, mảng hoặc hàm thể hiện các phím để nhóm theo
  • điều kiện (đối tượng) – một điều kiện tùy chọn phải đúng đối với một hàng được xem xét
  • ban đầu (đối tượng) - giá trị ban đầu của đối tượng bộ đếm tổng hợp
  • giảm (hàm) – hàm giảm tổng hợp (giảm) các đối tượng được lặp lại
  • hoàn thiện (chức năng) – một chức năng tùy chọn sẽ được chạy trên từng mục trong tập kết quả ngay trước khi mục đó được trả về
  • lệnh (boolean) – chỉ định nếu bạn muốn chạy bằng lệnh nhóm nội bộ hoặc sử dụng eval, mặc định là đúng
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình cập nhật
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức nhóm hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Rất nhiều cách khác nhau để thực hiện lệnh nhóm

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));
  // Only run the rest of the code if we have a mongodb server with version >= 1.9.1
  db.open(function(err, db) {

    // Create a collection
    var collection = db.collection('keepGoingExample');

    // Add an unique index to title to force errors in the batch insert
    collection.ensureIndex({title:1}, {unique:true}, function(err, indexName) {

      // Insert some intial data into the collection
      collection.insert([{name:"Jim"}
        , {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) {

        // Force keep going flag, ignoring unique index issue
        collection.insert([{name:"Jim"}
          , {name:"Sarah", title:"Princess"}
          , {name:'Gump', title:"Gump"}], {w:1, keepGoing:true}, function(err, result) {

          // Count the number of documents left (should not include the duplicates)
          collection.count(function(err, count) {
            assert.equal(3, count);
          })
        });
      });
    });
  });
2

tùy chọn

Trả về các tùy chọn của bộ sưu tập

tùy chọn (gọi lại) Đối số
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức tùy chọn hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Một ví dụ trả về các tùy chọn cho một bộ sưu tập

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));
  // Only run the rest of the code if we have a mongodb server with version >= 1.9.1
  db.open(function(err, db) {

    // Create a collection
    var collection = db.collection('keepGoingExample');

    // Add an unique index to title to force errors in the batch insert
    collection.ensureIndex({title:1}, {unique:true}, function(err, indexName) {

      // Insert some intial data into the collection
      collection.insert([{name:"Jim"}
        , {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) {

        // Force keep going flag, ignoring unique index issue
        collection.insert([{name:"Jim"}
          , {name:"Sarah", title:"Princess"}
          , {name:'Gump', title:"Gump"}], {w:1, keepGoing:true}, function(err, result) {

          // Count the number of documents left (should not include the duplicates)
          collection.count(function(err, count) {
            assert.equal(3, count);
          })
        });
      });
    });
  });
3

Bị giới hạn

Trả về nếu bộ sưu tập là bộ sưu tập giới hạn

Đối số isCapped (gọi lại)
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức isCapped hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Một ví dụ cho thấy cách thiết lập nếu đó là một bộ sưu tập giới hạ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));
  // Only run the rest of the code if we have a mongodb server with version >= 1.9.1
  db.open(function(err, db) {

    // Create a collection
    var collection = db.collection('keepGoingExample');

    // Add an unique index to title to force errors in the batch insert
    collection.ensureIndex({title:1}, {unique:true}, function(err, indexName) {

      // Insert some intial data into the collection
      collection.insert([{name:"Jim"}
        , {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) {

        // Force keep going flag, ignoring unique index issue
        collection.insert([{name:"Jim"}
          , {name:"Sarah", title:"Princess"}
          , {name:'Gump', title:"Gump"}], {w:1, keepGoing:true}, function(err, result) {

          // Count the number of documents left (should not include the duplicates)
          collection.count(function(err, count) {
            assert.equal(3, count);
          })
        });
      });
    });
  });
4

chỉ mụcTồn tại

Kiểm tra nếu một hoặc nhiều chỉ mục tồn tại trên bộ sưu tập

indexExists(indexNames, callback)Đối số
  • indexNames (string) – kiểm tra xem một hoặc nhiều chỉ mục có tồn tại trên bộ sưu tập không
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức indexExists hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Một ví dụ cho thấy việc sử dụng hàm indexExists cho một tên chỉ mục và danh sách các tên chỉ mục

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));
  // Only run the rest of the code if we have a mongodb server with version >= 1.9.1
  db.open(function(err, db) {

    // Create a collection
    var collection = db.collection('keepGoingExample');

    // Add an unique index to title to force errors in the batch insert
    collection.ensureIndex({title:1}, {unique:true}, function(err, indexName) {

      // Insert some intial data into the collection
      collection.insert([{name:"Jim"}
        , {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) {

        // Force keep going flag, ignoring unique index issue
        collection.insert([{name:"Jim"}
          , {name:"Sarah", title:"Princess"}
          , {name:'Gump', title:"Gump"}], {w:1, keepGoing:true}, function(err, result) {

          // Count the number of documents left (should not include the duplicates)
          collection.count(function(err, count) {
            assert.equal(3, count);
          })
        });
      });
    });
  });
5

địa lý gần

Thực hiện lệnh geoNear để tìm kiếm các mục trong bộ sưu tập

Tùy chọn
  • num {Number}, số kết quả trả về tối đa
  • minDistance {Number}, bao gồm các kết quả bắt đầu từ minDistance từ một điểm (2. 6 hoặc cao hơn)
  • maxDistance {Number}, bao gồm các kết quả lên tới maxDistance từ điểm
  • distanceMultiplier {Number}, bao gồm một giá trị để nhân khoảng cách với việc cho phép chuyển đổi phạm vi
  • truy vấn {Object}, lọc kết quả theo truy vấn
  • hình cầu {Boolean, mặc định. false}, thực hiện truy vấn bằng mô hình hình cầu
  • uniqueDocs {Boolean, mặc định. false}, vị trí gần nhất trong tài liệu với trung tâm của vùng tìm kiếm sẽ luôn được trả về MongoDB > 2. X
  • bao gồmLocs {Boolean, mặc định. false}, bao gồm các trường dữ liệu vị trí ở cấp cao nhất của kết quả MongoDB > 2. X
  • readPreference {String}, tùy chọn đọc ưu tiên, yêu cầu ('mongodb'). ReadPreference ((ReadPreference. CHÍNH, ReadPreference. PRIMARY_PREFERRED, ReadPreference. TRUNG HỌC, ReadPreference. SECONDARY_PREFERRED, ReadPreference. GẦN NHẤT)
geoNear(x, y [ , tùy chọn ] .
  • x (số) – trỏ để tìm kiếm trên trục x, đảm bảo các chỉ mục được sắp xếp theo cùng một thứ tự
  • y (số) – trỏ để tìm kiếm trên trục y, đảm bảo các chỉ mục được sắp xếp theo cùng một thứ tự
  • [options] (đối tượng) – tùy chọn cho bản đồ giảm công việc
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức GeoNear hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Ví dụ về truy vấn GeoNear đơn giản trên một số tài liệu

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));
  // Only run the rest of the code if we have a mongodb server with version >= 1.9.1
  db.open(function(err, db) {

    // Create a collection
    var collection = db.collection('keepGoingExample');

    // Add an unique index to title to force errors in the batch insert
    collection.ensureIndex({title:1}, {unique:true}, function(err, indexName) {

      // Insert some intial data into the collection
      collection.insert([{name:"Jim"}
        , {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) {

        // Force keep going flag, ignoring unique index issue
        collection.insert([{name:"Jim"}
          , {name:"Sarah", title:"Princess"}
          , {name:'Gump', title:"Gump"}], {w:1, keepGoing:true}, function(err, result) {

          // Count the number of documents left (should not include the duplicates)
          collection.count(function(err, count) {
            assert.equal(3, count);
          })
        });
      });
    });
  });
6

địa lýHaystackTìm kiếm

Thực hiện tìm kiếm địa lý bằng chỉ mục địa lý haystack trên bộ sưu tập

Tùy chọn
  • maxDistance {Number}, bao gồm các kết quả lên tới maxDistance từ điểm
  • tìm kiếm {Object}, lọc kết quả theo truy vấn
  • giới hạn {Number}, số lượng kết quả trả về tối đa
  • readPreference {String}, tùy chọn đọc ưu tiên, yêu cầu ('mongodb'). ReadPreference ((ReadPreference. CHÍNH, ReadPreference. PRIMARY_PREFERRED, ReadPreference. TRUNG HỌC, ReadPreference. SECONDARY_PREFERRED, ReadPreference. GẦN NHẤT)
geoHaystackSearch(x, y [ , tùy chọn ] .
  • x (số) – trỏ để tìm kiếm trên trục x, đảm bảo các chỉ mục được sắp xếp theo cùng một thứ tự
  • y (số) – trỏ để tìm kiếm trên trục y, đảm bảo các chỉ mục được sắp xếp theo cùng một thứ tự
  • [options] (đối tượng) – tùy chọn cho bản đồ giảm công việc
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức geoHaystackSearch hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Ví dụ về truy vấn GeoHaystackSearch đơn giản trên một số tài liệu

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));
  // Only run the rest of the code if we have a mongodb server with version >= 1.9.1
  db.open(function(err, db) {

    // Create a collection
    var collection = db.collection('keepGoingExample');

    // Add an unique index to title to force errors in the batch insert
    collection.ensureIndex({title:1}, {unique:true}, function(err, indexName) {

      // Insert some intial data into the collection
      collection.insert([{name:"Jim"}
        , {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) {

        // Force keep going flag, ignoring unique index issue
        collection.insert([{name:"Jim"}
          , {name:"Sarah", title:"Princess"}
          , {name:'Gump', title:"Gump"}], {w:1, keepGoing:true}, function(err, result) {

          // Count the number of documents left (should not include the duplicates)
          collection.count(function(err, count) {
            assert.equal(3, count);
          })
        });
      });
    });
  });
7

chỉ số

Truy xuất tất cả các chỉ mục trên bộ sưu tập

chỉ mục (gọi lại) Đối số
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức indexes hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Ví dụ về truy xuất chỉ mục bộ sưu tập

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));
  // Only run the rest of the code if we have a mongodb server with version >= 1.9.1
  db.open(function(err, db) {

    // Create a collection
    var collection = db.collection('keepGoingExample');

    // Add an unique index to title to force errors in the batch insert
    collection.ensureIndex({title:1}, {unique:true}, function(err, indexName) {

      // Insert some intial data into the collection
      collection.insert([{name:"Jim"}
        , {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) {

        // Force keep going flag, ignoring unique index issue
        collection.insert([{name:"Jim"}
          , {name:"Sarah", title:"Princess"}
          , {name:'Gump', title:"Gump"}], {w:1, keepGoing:true}, function(err, result) {

          // Count the number of documents left (should not include the duplicates)
          collection.count(function(err, count) {
            assert.equal(3, count);
          })
        });
      });
    });
  });
8

tổng hợp

Thực thi một đường dẫn khung tổng hợp đối với bộ sưu tập, cần MongoDB >= 2. 2

Tùy chọn
  • readPreference {String}, tùy chọn đọc ưu tiên, yêu cầu ('mongodb'). ReadPreference ((ReadPreference. CHÍNH, ReadPreference. PRIMARY_PREFERRED, ReadPreference. TRUNG HỌC, ReadPreference. SECONDARY_PREFERRED, ReadPreference. GẦN NHẤT)
  • con trỏ {Object}, trả về truy vấn dưới dạng con trỏ, trên 2. 6 > nó trả về dưới dạng con trỏ thực trên 2. 6, nó trả về dưới dạng con trỏ mô phỏng
  • con trỏ. batchSize {Number}, batchSize cho con trỏ
  • out {String}, tên tập hợp để ghi kết quả từ tập hợp (MongoDB 2. 6 hoặc cao hơn). Cảnh báo mọi bộ sưu tập hiện có sẽ bị ghi đè
  • giải thích {Boolean, mặc định. false}, giải thích trả về kế hoạch thực hiện tổng hợp (yêu cầu mongodb 2. 6 >)
  • allowDiskUse {Boolean, mặc định. false}, allowDiskUse cho phép máy chủ biết liệu nó có thể sử dụng đĩa để lưu kết quả tạm thời cho tập hợp hay không (yêu cầu mongodb 2. 6 >)
aggregate(array [ , tùy chọn ] , gọi lại .
  • mảng (array) – chứa tất cả các lệnh khung tổng hợp để thực thi
  • [tùy chọn] (đối tượng) – tùy chọn bổ sung trong quá trình cập nhật
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức tổng hợp hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Gọi chính xác khung tổng hợp bằng cách sử dụng đường dẫn trong Mảng

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));
  // Only run the rest of the code if we have a mongodb server with version >= 1.9.1
  db.open(function(err, db) {

    // Create a collection
    var collection = db.collection('keepGoingExample');

    // Add an unique index to title to force errors in the batch insert
    collection.ensureIndex({title:1}, {unique:true}, function(err, indexName) {

      // Insert some intial data into the collection
      collection.insert([{name:"Jim"}
        , {name:"Sarah", title:"Princess"}], {w:1}, function(err, result) {

        // Force keep going flag, ignoring unique index issue
        collection.insert([{name:"Jim"}
          , {name:"Sarah", title:"Princess"}
          , {name:'Gump', title:"Gump"}], {w:1, keepGoing:true}, function(err, result) {

          // Count the number of documents left (should not include the duplicates)
          collection.count(function(err, count) {
            assert.equal(3, count);
          })
        });
      });
    });
  });
9

Gọi chính xác khung tổng hợp bằng cách sử dụng đường dẫn được biểu thị dưới dạng danh sách đối số

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) {

  // Fetch a collection to insert document into
  db.collection("remove_all_documents_no_safe", function(err, collection) {

    // Insert a bunch of documents
    collection.insert([{a:1}, {b:2}], {w:1}, function(err, result) {
      assert.equal(null, err);

      // Remove all the document
      collection.remove();

      // Fetch all results
      collection.find().toArray(function(err, items) {
        assert.equal(null, err);
        assert.equal(0, items.length);
        db.close();
      });
    });
  })
});
0

Gọi chính xác khung tổng hợp bằng cách sử dụng đường dẫn được biểu thị dưới dạng danh sách đối số

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) {

  // Fetch a collection to insert document into
  db.collection("remove_all_documents_no_safe", function(err, collection) {

    // Insert a bunch of documents
    collection.insert([{a:1}, {b:2}], {w:1}, function(err, result) {
      assert.equal(null, err);

      // Remove all the document
      collection.remove();

      // Fetch all results
      collection.find().toArray(function(err, items) {
        assert.equal(null, err);
        assert.equal(0, items.length);
        db.close();
      });
    });
  })
});
0

Gọi chính xác khung tổng hợp để trả về một 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) {

  // Fetch a collection to insert document into
  db.collection("remove_all_documents_no_safe", function(err, collection) {

    // Insert a bunch of documents
    collection.insert([{a:1}, {b:2}], {w:1}, function(err, result) {
      assert.equal(null, err);

      // Remove all the document
      collection.remove();

      // Fetch all results
      collection.find().toArray(function(err, items) {
        assert.equal(null, err);
        assert.equal(0, items.length);
        db.close();
      });
    });
  })
});
2

Gọi chính xác khung tổng hợp để trả về một con trỏ và gọi giải thích

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) {

  // Fetch a collection to insert document into
  db.collection("remove_all_documents_no_safe", function(err, collection) {

    // Insert a bunch of documents
    collection.insert([{a:1}, {b:2}], {w:1}, function(err, result) {
      assert.equal(null, err);

      // Remove all the document
      collection.remove();

      // Fetch all results
      collection.find().toArray(function(err, items) {
        assert.equal(null, err);
        assert.equal(0, items.length);
        db.close();
      });
    });
  })
});
3

Gọi chính xác khung tổng hợp để trả về con trỏ có kích thước lô 1 và nhận kết quả đầu tiên bằng cách sử dụng tiếp theo

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) {

  // Fetch a collection to insert document into
  db.collection("remove_all_documents_no_safe", function(err, collection) {

    // Insert a bunch of documents
    collection.insert([{a:1}, {b:2}], {w:1}, function(err, result) {
      assert.equal(null, err);

      // Remove all the document
      collection.remove();

      // Fetch all results
      collection.find().toArray(function(err, items) {
        assert.equal(null, err);
        assert.equal(0, items.length);
        db.close();
      });
    });
  })
});
4

Gọi chính xác khung tổng hợp và ghi kết quả vào bộ sưu tập mớ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) {

  // Fetch a collection to insert document into
  db.collection("remove_all_documents_no_safe", function(err, collection) {

    // Insert a bunch of documents
    collection.insert([{a:1}, {b:2}], {w:1}, function(err, result) {
      assert.equal(null, err);

      // Remove all the document
      collection.remove();

      // Fetch all results
      collection.find().toArray(function(err, items) {
        assert.equal(null, err);
        assert.equal(0, items.length);
        db.close();
      });
    });
  })
});
5

Sử dụng đúng allowDiskUse khi thực hiện tổng hợp

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) {

  // Fetch a collection to insert document into
  db.collection("remove_all_documents_no_safe", function(err, collection) {

    // Insert a bunch of documents
    collection.insert([{a:1}, {b:2}], {w:1}, function(err, result) {
      assert.equal(null, err);

      // Remove all the document
      collection.remove();

      // Fetch all results
      collection.find().toArray(function(err, items) {
        assert.equal(null, err);
        assert.equal(0, items.length);
        db.close();
      });
    });
  })
});
6

số liệu thống kê

Nhận tất cả các số liệu thống kê bộ sưu tập

Tùy chọn
  • chia tỷ lệ {Number}, chia kích thước được trả về theo giá trị tỷ lệ
  • readPreference {String}, tùy chọn đọc ưu tiên, yêu cầu ('mongodb'). ReadPreference ((ReadPreference. CHÍNH, ReadPreference. PRIMARY_PREFERRED, ReadPreference. TRUNG HỌC, ReadPreference. SECONDARY_PREFERRED, ReadPreference. GẦN NHẤT)
số liệu thống kê( [ tùy chọn ] , gọi lại)Đối số.
  • [tùy chọn] (đối tượng) – tùy chọn cho lệnh thống kê
  • gọi lại (hàm) – cái 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 nếu không. Trong khi tham số thứ hai sẽ chứa kết quả từ phương thức thống kê hoặc null nếu xảy ra lỗi
trả lại

vô giá trị

ví dụ

Ví dụ về truy xuất số liệu thống kê bộ sưu tập

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) {

  // Fetch a collection to insert document into
  db.collection("remove_all_documents_no_safe", function(err, collection) {

    // Insert a bunch of documents
    collection.insert([{a:1}, {b:2}], {w:1}, function(err, result) {
      assert.equal(null, err);

      // Remove all the document
      collection.remove();

      // Fetch all results
      collection.find().toArray(function(err, items) {
        assert.equal(null, err);
        assert.equal(0, items.length);
        db.close();
      });
    });
  })
});
7

khởi tạoUnorderedBulkOp

Bắt đầu thao tác ghi hàng loạt không theo thứ tự. Tất cả các hoạt động sẽ được đệm vào các lệnh chèn/cập nhật/xóa được thực hiện không theo thứ tự

Tùy chọn
  • w, {Số/Chuỗi, > -1. 'số đông'. tên thẻ} mối quan tâm ghi cho hoạt động trong đó < 1 không xác nhận ghi và w >= 1, w = ‘đa số’ hoặc thẻ xác nhận ghi
  • wtimeout, {Number, 0} đặt thời gian chờ đợi ghi mối quan tâm kết thúc (kết hợp với tùy chọn w)
  • fsync, (Boolean, mặc định. false) ghi đợi fsync trước khi quay lại, từ MongoDB 2. 6 bật, không thể kết hợp fsync với tạp chí
  • j, (Boolean, mặc định. sai) viết chờ đồng bộ nhật ký trước khi quay lại
initializeUnorderedBulkOp( [ options ] , gọi lại)Đối số.
  • [tùy chọn] (đối tượng) – tùy chọn cho khởi tạoUnorderedBatch
  • gọi lại (hàm) – cái 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 nếu không. Đối số thứ hai sẽ là một đối tượng UnorderedBulkOperation
trả lại

hoạt động hàng loạt không có thứ tự

khởi tạoOrderedBulkOp

Bắt đầu một thao tác ghi hàng loạt Theo thứ tự, các thao tác sẽ được thực hiện tuần tự theo thứ tự chúng được thêm vào, tạo một thao tác mới cho mỗi công tắc theo loại

Tùy chọn
  • w, {Số/Chuỗi, > -1. 'số đông'. tên thẻ} mối quan tâm ghi cho hoạt động trong đó < 1 không xác nhận ghi và w >= 1, w = ‘đa số’ hoặc thẻ xác nhận ghi
  • wtimeout, {Number, 0} đặt thời gian chờ đợi ghi mối quan tâm kết thúc (kết hợp với tùy chọn w)
  • fsync, (Boolean, mặc định. false) ghi đợi fsync trước khi quay lại, từ MongoDB 2. 6 bật, không thể kết hợp fsync với tạp chí
  • j, (Boolean, mặc định. sai) viết chờ đồng bộ nhật ký trước khi quay lại
initializeOrderedBulkOp( [ options ] , gọi lại)Đối số.
  • [tùy chọn] (đối tượng) – tùy chọn cho khởi tạoOrderedBulkOp
  • gọi lại (hàm) – cái 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 nếu không. Đối số thứ hai sẽ là đối tượng OrderedBulkOperation
trả lại

đặt hàng số lượng lớn hoạt động

song songBộ sưu tậpQuét

Trả về N số con trỏ song song cho một bộ sưu tập cho phép đọc song song toàn bộ bộ sưu tập. Không có đảm bảo đặt hàng cho kết quả trả lại

Làm cách nào để lấy dữ liệu từ 2 bộ sưu tập MongoDB khác nhau bằng nút js?

Sau khi có mô hình, chúng ta có thể sử dụng phương thức find() trên mô hình của một bộ sưu tập cụ thể để lấy tài liệu của bộ sưu tập .

Làm cách nào để lấy dữ liệu từ 2 bộ sưu tập khác nhau trong MongoDB?

MongoDB tham gia hai truy vấn bộ sưu tập . Trong phần này, bạn phải chỉ định bộ sưu tập nào bạn muốn kết hợp với bộ sưu tập hiện tại và chọn trường khớp với cả hai bộ sưu tập. through the $lookup aggregation stage. In this, you have to specify which collection you want to join with the current collection and select the field that matches in both the collection.

Làm cách nào để tham gia hai bảng trong nút js?

Tham gia hai hoặc nhiều bàn . by using a JOIN statement.

Chúng tôi có thể tham gia các bộ sưu tập trong MongoDB không?

Có, bạn có thể tham gia 2 bộ sưu tập với Khung tổng hợp và giai đoạn $unionWith . Dưới đây là các tài liệu với cú pháp và ví dụ, vì vậy bạn có thể kiểm tra cách thực hiện.