【问题标题】:How Do i connect sails to mongodb?我如何将风帆连接到 mongodb?
【发布时间】:2016-04-12 05:34:46
【问题描述】:

我不熟悉sails Js 和mongodb。我绝对是个新手。

问题: 我的 mongodb 数据库上已经有一个用户集合。我想将它连接到风帆以显示收藏列表。

我看过sails js 文档。我已经安装了sails-mongo 适配器并编辑了connection.js ....

 module.exports = {
    // this is my model
  attributes: {

    firstname: {
    type: 'string'
    }


  },
  findUser: function(opts,callback){
      ///// How am I going to connect to mongo and query the users ?

     //// user.find('John Doe') wont work here.
    }

 }

【问题讨论】:

    标签: javascript mongodb sails.js nosql


    【解决方案1】:
    1. 运行:npm install sails-mongo
    2. config/connections.js取消注释与mongo相关的部分并在那里输入必要的信息

      someMongodbServer: { 适配器:'sails-mongo', 主机:'mongohost.com', 端口:55915, 用户:'用户', 密码:'密码', 数据库:'dbname' },

    3. config/models.js 中输入你的连接适配器变量

      ... 连接:'someMongodbServer', ...

    4. 完成

    编辑: 为了从数据库中获取数据,请使用以下命令:

    User.find({firstname: 'John Doe'}).exec(function(error, user) {
        console.log(user);
    });
    

    【讨论】:

    • 我需要添加一些东西来从我的 mongo 中获取数据集合/表吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 2017-09-15
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多