k668 发表于 2015-11-11 07:35:05

Mongo DB编程入门

  连接数据库
  直接上代码
  

// Retrieve
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
if(!err) {
console.log("We are connected");
}
})
  poolSize,默认的连接数为5,通过poolSize修改。
  


  创建集合
  

// Retrieve
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
if(err) { return console.dir(err); }
db.collection('test', function(err, collection) {});
db.collection('test', {w:1}, function(err, collection) {});
db.createCollection('test', function(err, collection) {});
db.createCollection('test', {w:1}, function(err, collection) {});
});
有三种创建集合的方法  
  方法一:
  

db.collection('test', function(err, collection) {});
该方法并不立马创建集合,而是在首次插入文档时创建。  
  方法二:
  

db.collection('test', {strict:true}, function(err, collection) {});
该方法在{strict:true}参数下,严格检查集合是否已经存在,如果已经存在会报错。  
  方法三:
  

db.createCollection('test', function(err, collection) {});
该方法将在返回集合对象前创建对象,如果该集合已经存在,则不创建。  
  

db.createCollection('test', {strict:true}, function(err, collection) {});
该创建方式将严格检查集合是否已经存在,如果已经存在则报错。  
  


  下面介绍增删改查(CRUD)
  


  


  


  
  


  


  


  翻译的页面
  http://mongodb.github.io/node-mongodb-native/api-articles/nodekoarticle1.html#getting-that-connection-to-the-database


  (后面慢慢完善)
页: [1]
查看完整版本: Mongo DB编程入门