dot_zero
dot_zero

Reputation: 1040

How do I convert a regular JSON object into a Mongo document using Mongoose?

I have a JSON Object called parsedSong that I want to convert into a Mongo document using Mongoose.

var newSong = new Song(parsedSong);

However, this only provides me with a new document that only has an id attribute. How can I make a conversion that doesn't cut my data from parsedSong?

Upvotes: 0

Views: 1084

Answers (1)

Prontto
Prontto

Reputation: 1681

I assumed you have defined these variables in schema. Then you can make something like this:

var bestSong = {
    artist: "Seether",
    song: "Careless Whisper"
};

var song = new Song(bestSong);

song.save(function(err) {
    if(err) throw err;
    console.log('saved');
});

And now this song should be in database.

Upvotes: 1

Related Questions