【发布时间】:2015-05-17 00:01:41
【问题描述】:
我正在使用 node.js 将数据存储在 MongoDB 集合中。因为我想使用变量将数据存储在我使用对象的正确位置,例如this
我的问题如下:我想将数据推送到 2 个不同的数组中,即“温度”中的一个数组。和“时代”中的一个数组。通常应该使用一个 $push 并定义所有需要的“推送”,就像我对 $inc 所做的那样。不幸的是,我不能一次推送 2 个对象,我的代码只推送最后一个对象(在本例中为“时间”)。希望有人可以帮助我!
这是我的 node.js 文件:
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();
这是我的 JSON 文件:
{ "_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
}
}
【问题讨论】:
-
尝试:
$push: {Times: time, Temperature: temp} -
这不起作用并导致 $set 和 $inc 停止工作。没有抛出错误...