2012-11-17 118 views
0

我有這個集合名稱的一個問題:的MongoDB Node.js的

module.exports = function() { 

    var mongoose = require('mongoose'); 
    var db = mongoose.createConnection('localhost', 'race'); 
    db.on('error', console.error.bind(console, 'connection error:')); 
    db.once('open', function() {}); 

    var collection = 'test'; 
    var Schema = mongoose.Schema; 
    var ObjectId = Schema.ObjectId; 

    var schema = new Schema({ 
     author: ObjectId, 
     name: String, 
     date: Date 
    }); 

    this.model = db.model(collection, schema); 

    var silence = new this.model({ name: 'Silence' }) 
    console.log(silence.name); 
    silence.save(); 

    this.model.find(function (err, log) { 
     console.log(err) 
     console.log(log) 
    }) 

    return this; 
}; 

我已經有一個測試集,但執行console.log(日誌)只返回沉默,

其實沉默是登記「測試」的收集和沒有「測試」

你能解釋一下我爲什麼,我設置

var collection = 'test'; 

回答

2

試試這個

var schema = new Schema({ 
     author: ObjectId, 
     name: String, 
     date: Date 
    }, { collection: collection }); 
+0

It works thanks :) – Ajouve