girishs
girishs

Reputation: 121

How to store/retrieve into mongodb using mongoose ORM

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 code:

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

Upvotes: 2

Views: 1888

Answers (3)

Vitaly Kushner
Vitaly Kushner

Reputation: 9455

no need to create artist_doc

just do artist.save

Upvotes: 1

Alex Craft
Alex Craft

Reputation: 15336

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

Upvotes: 1

Troy
Troy

Reputation: 1649

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

Upvotes: 1

Related Questions