2014-06-25 17 views
2

我想在sails上啓動幾個db-command。像:db.dropDatabase(),db.repairDatabase(),...風帆:訪問本地mongodb驅動程序

我知道你可以使用

mymodel.native() 

鏈接至db.collection( 「爲MyModel」)

但如何我可以直接訪問數據庫對象?

+2

我們目前不暴露db對象只集合對象來自'native'命令。你可以通過查看這裏的'native'函數來修補適配器來添加它:https://github.com/balderdashy/sails-mongo/blob/master/lib/adapter.js#L212-L217 – particlebanana

+0

你找到了嗎?比直接使用mongo驅動程序更好的解決方案? – Simoyw

回答

1

我在adapter.js增加了以下內容:

getDB: function(connectionName, collectionName, cb) { 
     var connectionObject = connections[connectionName]; 
     cb(null, connectionObject.connection.db); 
    }, 

,並用它作爲Model.getDB():

User.getDB(function (err,db){ 
     console.log('getDB'); 

     var collection = db.collection('test'); 
     // Insert some documents 
     collection.insert([ {a : 1}, {a : 2}, {a : 3}], function(err, result) { 
      console.log("Inserted 3 documents into the document collection", result); 
     }); 

     return res.json({ 
      notice: 'Inserted 3 documents into the document collection' 
     }); 

     }); 
    } 
+0

而不是直接添加到node_modules中,我們可以在Config/adapter.js中擴展相同的...任何人都有如何做到這一點的想法。 –