0

So I have a model which contains this as part of it. I'm trying to insert records that are categorized by day("7/20/2017", "7/21/2017"), and then individually time stamped.

My issue is how do you upsert in Mongoose to not only push the record, but create the parent/observation if needed

observations : [
    {
        date : {type: Date, default: new Date().toLocaleString().split(',')[0]},
        record : 
        [
            {
              category : String,
              timestamp : {type: Date, default: Date.now()},
            }
        ]
    }
],

Here's my current update code for pushing records on the current day, but I'm not sure how to get the nested upsert to work when the observation with "date" doesn't exist:

    var todayISO = "7/21/2017"
    var observation = {
        "category": "Diaper",
        "timestamp": todayISO,
    }


Child.findOneAndUpdate({"child.firstName.name": studentName, "child.observations.date": todayISO} , {$push: {"child.observations.$.record": observation}}, {upsert: true, new: true}, function(err,result){
    if(err){
        console.log("error");
        return res.status(500).json({err: "Couldn't append record"});
    }
    else {
        console.log("added");
        return res.json({'record': observation, message: 'Added record under ' + observation.category});
    }
});

So that this:

"observations":
[
        {
            date: "2017-07-20T04:00:00.000Z",
            "record":
            [
                {"category":"Diaper",timestamp: "2017-07-06T21:04:30.337Z"}
            ]
        }

Becomes this when a user tries to log a record on a new day:

"observations":
[

        {
            date: "2017-07-20T04:00:00.000Z",
            "record":
            [
                {"category":"Diaper",timestamp: "2017-07-20T21:04:30.337Z"}
            ]
        },
        {
            date: "2017-07-21T04:00:00.000Z",
            "record":
            [
                {"category":"Nap",timestamp: "2017-07-21T10:04:30.337Z"}
            ]
        }

]
joshbenner851
  • 111
  • 1
  • 12
  • Don't nest arrays. This does not actually "categorize by day" at all. The "day" is actually already present on the `"timestamp"` field. So you are just duplicating data and in fact making things more difficult to update and query in the process. It's a common error ( far too common actually ) to "presume" this makes things better. But you are actually making things far worse. Simply remove the nesting and rely on the timestamp values instead. They are much easier to query, and then updates are not a problem. – Neil Lunn Jul 21 '17 at 04:12
  • The other "big mistake" I see here is that you appear to be attempting to coerce the UTC values into "locale" specific values. Don't do that either. Just rely on the "fact" that UTC is the stored format for a "reason". If that wisdom presently escapes your own reasoning, consider that others before you might just know better and that is why UTC is chosen. If you want "locale based reporting", then I suggest you take a read of ["Group by Date with Local Time Zone in MongoDB"](https://stackoverflow.com/a/45093686/2313887) where I took the time to craft a response that should explain this. – Neil Lunn Jul 21 '17 at 04:17
  • I have to handle live auditing, hence the redundancy of the dates. It also makes querying easier because when I need to see the last "nap" today, it's much easier to lookup by today, and then check the 10 records for today vs the 5000 records over a whole year if they're all grouped together. It's just a faster query. So do you know how to do this nested upsert? As I think it'd be useful for others on SO. Thanks for the time info however, I knew that needed some help but wasn't sure what to do. – joshbenner851 Jul 21 '17 at 05:04

0 Answers0