【问题标题】:How to $push 2 objects in the same update function in MongoDB如何在 MongoDB 的同一更新函数中 $push 2 个对象
【发布时间】: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 停止工作。没有抛出错误...

标签: node.js mongodb object


【解决方案1】:

您的代码未能构建一个 看起来$push 您的数据类似的对象:

{
  "Temperature.23": 12,
  "Times.23": 57
}

您可以通过对代码进行简单更改来获得所需的结果:

changes = { }
changes["Temperature."+hour]=content;
changes["Times."+hour]=minute;

然后:

{
  $push: changes,  // <----
  $set: {"Temperature.TotalAverage": "dif"}, //works
  ...
}

【讨论】:

  • 猜我一直在寻找解决方案。谢谢
猜你喜欢
  • 2012-02-22
  • 2015-05-07
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
相关资源
最近更新 更多