开发者

How to store/retrieve into mongodb using mongoose ORM

开发者 https://www.devze.com 2023-03-29 11:58 出处:网络
I am newbie to mongodb and mongoose orm. I wrote a sample coffeescript to store data into mongodb, but database is not created,

I am newbie to mongodb and mongoose orm. I wrote a sample coffeescript to store data into mongodb, but database is not created, Here is my co开发者_高级运维de:

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

After executing the above script, db is not created in mongodb.

Am I missing anything?

Regards, gms


I saw your comment, but wanted to suggest (for others that may find this) a way to do this with comprehensions that I think is preferable. Change the last three lines from:

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

to:

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


Sample how to use Mongo Model with CoffeeScript http://alexeypetrushin.github.com/mongo-model/basics.html


no need to create artist_doc

just do artist.save

0

精彩评论

暂无评论...
验证码 换一张
取 消