I'm using node.js to store data in a MongoDB collection. Because I want to use variables to store data in the correct place I'm using objects, like this
My problem is the following: I want to push data in 2 different arrays, an array in "Temperature." and an array in "Times." Normally one should use one $push and define all needed "pushes", just like I did with $inc. Unfortunately I'm not able to push 2 objects at once, my code only pushes the last object ("time" in this case). Hope someone can help me!
This is my node.js file:
var content = 22;
var t = new Date();
var hour = t.getHours();
var minute = t.getMinutes();
var temp = {};
var time = {};
temp["Temperature."+hour]=content;
time["Times."+hour]=minute;
mongodbClient.connect("mongodb://localhost:27017/database1", function(err,d$
if(err){return console.dir(err);}
collection=db.collection("collection1");
collection.update(
{ _id:"S5113N, 424E:20150513" },
{
//Everything works just fine untill:
$push:(temp,time), //Using { } won't work
$set: {"Temperature.TotalAverage": "dif"}, //works
$inc: { //works
"Temperature.Updates": 1,
"Times.Updates": 1
}
}
)
db.close();
This is my JSON file:
{ "_id": "S5113N, 424E:20150513",
"I/O": "Indoor",
"Times":{
"0": [],
"1": [],
"2": [],
"3": [],
"4": [],
"5": [],
"6": [],
"7": [],
"8": [],
"9": [],
"10": [],
"11": [],
"12": [],
"13": [],
"14": [],
"15": [],
"16": [],
"17": [],
"18": [],
"19": [],
"20": [],
"21": [],
"22": [],
"23": [],
"Updates": 0
},
"Temperature":{
"0": [],
"1": [],
"2": [],
"3": [],
"4": [],
"5": [],
"6": [],
"7": [],
"8": [],
"9": [],
"10": [],
"11": [],
"12": [],
"13": [],
"14": [],
"15": [],
"16": [],
"17": [],
"18": [],
"19": [],
"20": [],
"21": [],
"22": [],
"23": [],
"Averages": [],
"TotalAverage": 0,
"Updates": 0
}
}