我是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
答案 0 :(得分: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 :(得分:1)
演示如何将Mongo模型与CoffeeScript http://alexeypetrushin.github.com/mongo-model/basics.html
一起使用答案 2 :(得分:1)
无需创建artist_doc
只做artist.save