teelou teelou - 1 month ago 8
Node.js Question

MoongooseJS - how to push the right format?

I want to create the data in this format:

{
"weather": {
"dailysummary": [...],
"created_on": "2016-10-27 11:11:11",
"addedOnLastHour": false
}
}


but why moongoose keeps giving me this format:

{
"weather": [
{
"dailysummary": [...],
"created_on": "2016-10-27 11:11:11",
"addedOnLastHour": false
}
]
}


My code:

var updateQuery = {};
var weather = {};
weather.dailysummary = [];
weather.created_on = '2016-10-27 11:11:11';
weather.addedOnLastHour = false;

// Insert date data.
updateQuery["weather"] = weather;

stream.update({
$push: updateQuery
}, function(err, streamID) {
if (err) {
// handle error
}
// success
});


What have I done wrong? Any ideas?

My model:

var mongoose = require("mongoose");
var mongoosePaginate = require('mongoose-paginate');

// Declare schema
var streamSchema = new mongoose.Schema({
title: {
type: String,
required: true
},
weather: {
type: Object
},
});

streamSchema.plugin(mongoosePaginate);

// Export schema
// Model.paginate()
mongoose.model("Stream", streamSchema);

Answer

you can use $set operator instead of $push

try this:

stream.update({
    $set: updateQuery
}, function(err, streamID) {
    if (err) {
        // handle error
    }
    // success
});