2016-10-13 4 views
1

Ich habe einige Kombinationen ausprobiert und die MongoDB 3.2 Dokumente geprüft, aber ich bekomme mein neues Date() immer noch nicht in meiner DB gespeichert. Alles andere wird richtig gespeichert. Vielen Dank im Voraus für irgendwelche Tipps!

-Code aus meiner app.js Datei:

db.collection('users').update({user: req.user.username}, { 
        $push: { 
         "recentPlays": { 
          "quiz": req.session.mostRecentQuiz, 
          "score": score 
         } 
        } 
       },{ 
        $set: { 
         "mostRecentQuiz.quizObj": req.session.mostRecentQuiz, 
         "mostRecentQuiz.time" : new Date() // StackOverflow: this is the part that is not getting stored in the DB. 
        } 
       }, function (err, result) { 
        if (err) throw err; 
        console.log(result); 
       }); 
+0

zum Speichern von Zeitstempel Versuchen anstelle des Objekts: "Date.now()" statt "new Date()" – Nosyara

+0

@Nosyara Dank für Ihre Antwort. Ich habe gerade versucht Date.now() und es funktioniert auch nicht. "new Date()" funktioniert an anderer Stelle in meiner App. –

Antwort

1

Ihre Update-Methode die falsche Art und Weise aufgerufen wird, die Update-Operatoren $set und $push sollten im selben Dokument sein. Re-schreiben Sie Ihre Methode als:

db.collection('users').update(
    { "user": req.user.username }, 
    { 
     "$push": { 
      "recentPlays": { 
       "quiz": req.session.mostRecentQuiz, 
       "score": score 
      } 
     } 
     "$set": { 
      "mostRecentQuiz.quizObj": req.session.mostRecentQuiz, 
      "mostRecentQuiz.time" : new Date() 
     } 
    }, function (err, result) { 
     if (err) throw err; 
     console.log(result); 
    } 
);