2011-08-18 110 views
2

我是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

回答

1

我看到你的評論,但希望建議(替他人可能發現這一點)的方式與我認爲最好解析來做到這一點。

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

到:從更改最後三行

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

無需創建artist_doc

只是做artist.save