如何使用mongoose ORM在mongodb中存储/检索

如何使用mongoose ORM在mongodb中存储/检索,mongodb,coffeescript,mongoose,Mongodb,Coffeescript,Mongoose,我是mongodb和mongoose orm的新手。我编写了一个示例coffeescript将数据存储到mongodb中,但并没有创建数据库, 这是我的密码: mongoose = require('mongoose') db = mongoose.connect('mongodb://localhost/test') people = [{ bio: 'hello1' first_name: 'jay' last_name: 'roger' },{ bi

我是mongodb和mongoose orm的新手。我编写了一个示例coffeescript将数据存储到mongodb中,但并没有创建数据库, 这是我的密码:

mongoose = require('mongoose')

db = mongoose.connect('mongodb://localhost/test')

people = [{
    bio: 'hello1'
    first_name: 'jay'
    last_name: 'roger'
  },{
    bio: 'hello2'
    first_name: 'jay'
    last_name: 'roger'
  }]

artist_schema = new mongoose.Schema
     bio: String
     first_name: String
     last_name: String

artist_model = mongoose.model "artist", artist_schema

artist_doc = new mongoose.Collection 'artists', db

for person in people
    artist = new artist_model person
    artist_doc.insert artist
执行上述脚本后,不会在mongodb中创建db

我遗漏了什么吗

问候,,
gms

我看到了你的评论,但我想建议(对于其他可能会发现这一点的人)一种我认为更可取的理解方式。将最后三行更改为:

for person in people
    artist = new artist_model person
    artist_doc.insert artist
致:


示例如何将Mongo模型与CoffeeScript一起使用无需创建艺术家文档


只需执行artist.save

即可获得存储数据的解决方案,正确的代码是:
对于人物中的人物:artist=new artist\u model person;艺术家保存()
artist_doc.insert new artist_model(person) for person in people