node.js---sails项目开发(4)---配置MongoDB数据库连接

1、安装sails对mongo的依赖

npm install sails-mongo --save

2、 配置mongo连接  

修改config/connections.js:

module.exports.connections = {

someMongodbServer: {
    adapter: 'sails-mongo',
    host: '127.0.0.1',
    port: 27017,
    user: 'test', //optional
    password: 'test', //optional
    database: 'sails' //optional
  }
};

3、 模型层的基本配置

修改config/models.js,配置模型的连接数据库位mongodb,并为每个模型添加updatedAt以及createdAt属性:

module.exports.models = {

  migrate: 'alter',
  'connection': 'someMongodbServer',
  autoCreatedAt: true,
  autoUpdatedAt: true
};

 

下面对migrate进行一些说明:

safe - never auto-migrate my database(s). I will do it myself (by hand)[不自动合并数据,需要手动控制]
alter - auto-migrate, but attempt to keep my existing data (experimental)[与老数据自动合并,当添加新字段后,数据表才会被删除,推荐使用]
drop - wipe/drop ALL my data and rebuild models every time I lift Sails[删除数据表,建立新表,插入新数据]

4、修改config/env/development.js

 

module.exports = {
  models: {
    connection: 'someMongodbServer'
  }

};

5、 接下来,我们启动sails

sails lift

  

 

转载于:https://www.cnblogs.com/shenggen/p/5435657.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值