Home  >  Q&A  >  body text

node.js - 怎么使用mongolass驱动库连接mongodb数据库呀?

exports.User = mongolass.model('User', {
name: { type: 'string' },
password: { type: 'string' },
avatar: { type: 'string' },
gender: { type: 'string', enum: ['m', 'f', 'x'] },
bio: { type: 'string' }
});
exports.User.index({ name: 1 }, { unique: true }).exec();// 根据用户名找到用户,用户名全局唯一

这是什么意思呀?丝毫不懂

巴扎黑巴扎黑2716 days ago603

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 16:34:18

    Why not use mongoose? There are many tutorials on this

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 16:34:18

    It means that a collection model has been established to define the fields and the value types of the fields. Previously, it corresponded to the collection Users in Mongodb. After mongolass2.4.5, it corresponds to the collection named User in the database. All subsequent additions, deletions, and modifications will be checked through this Model object operations, such as User.insert User.find, etc.
    The following sentence

    exports.User.index({ name: 1 }, { unique: true }).exec()

    means that an index in positive order by name has been established and cannot be repeated

    As for how to connect, it is in the original author's document. The configuration is written in default.js in the config. Then use mongolass.connect(config.mongodb); and you will be connected

    reply
    0
  • Cancelreply