简体   繁体   中英

How to $push 2 objects in the same update function in MongoDB

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
        }

}

Your code failed to build an object that looks like that to $push your data:

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

You can achieve the desired result with a simple change in your code:

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

Then:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM