jeudi 19 février 2015

How to create field using variable name?

How can I create a field in mongoose using a variable name? I've seen some ways to do it using .update(), but I was wondering if there was a way to do it when creating a new document


I have my schema like:



var summariesSchema = mongoose.Schema({
type: String,
name: String,
date: String
})


and my object:



var date = '2015-02-01'
var obj = {
ios: 100,
android: 500
}
var doc = {}
doc[date] = obj
var mongoDoc = new Summaries({
name: 'John',
type: 'person',
date: date,
$doc: doc
})
mongoDoc.save(function(err){
if(!err) log('done')
else log(err.toString())
})


But it only saves the fields 'name', 'type' and 'date'.


Can anyone tell me if its possible to do something like that and if so, what am I missing?


Aucun commentaire:

Enregistrer un commentaire