如何使用猫鼬ORM存储/检索到mongodb

问题描述:

我是mongodb和mongoose orm的新手。我写了一个样本的CoffeeScript将数据存储到MongoDB中,但不创建数据库, 这里是我的代码:如何使用猫鼬ORM存储/检索到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是不会创建数据库。

我错过了什么?

问候, 克

+1

得到了存储数据的解决方案,正确的代码是: '对于人的人: artist = new artist_model person; artist.save()' – girishs

我看到你的评论,但希望建议(替他人可能发现这一点)的方式与我认为最好解析来做到这一点。

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

到:从更改最后三行

artist_doc.insert new artist_model(person) for person in people 

示例如何使用蒙戈模型的CoffeeScript http://alexeypetrushin.github.com/mongo-model/basics.html

无需创建artist_doc

只是做artist.save