使用mongoose更新mongodb中的文档

时间:2018-06-13 03:39:17

标签: javascript node.js mongodb mongoose

我正在尝试使用mongoose更新mongodb中的集合。

function check (db) {

    var hours = 60 * 60 * 1000
    var threeHours = 3 * hours;

    var lastUpdated = null;

    db.collection("profile").find({userName: "Rick"}).toArray(function(err, result){

        var results = result[0]
        var currentTime = new Date().getTime();
        lastUpdated = new Date(results.lastUpdated).getTime();
        var totalPoints = results.points.free + results.points.purchased;

        var timeSinceLastUpdate =  currentTime - lastUpdated;


        // console.log(timeSinceLastUpdate)
        if (timeSinceLastUpdate >= threeHours) {
            // ... time to update score ... 
            if(totalPoints < 200){
                results.free += 50; // issue 50 free points
                db.collection("profile").insert({
                    points: {
                        free: results.free
                    }
                }, function(err, res){
                    if(err) throw err;

                    console.log(res)
                })
        } else { 
            // do nothing, wait for next check  

        }

    })

console

{ result: { ok: 1, n: 1 },
  ops: [ { points: [Object], _id: 5b2091161dc9ac7f916ec67f } ],
  insertedCount: 1,
  insertedIds: { '0': 5b2091161dc9ac7f916ec67f } }

但是实际的数据库没有得到更新..请纠正我在这里做错了什么。

非常感谢任何帮助。

编辑: 这是我的模特

var mongoose = require('mongoose');

var profileSchema = new mongoose.Schema({
    userName: String,
    points: {
        free: Number,
        purchased: Number
    },
    itemsPurchased: [{
        name: String
    }],
    itemsAvailable: [{
        name: String,
        points: Number
    }],
    accountCreated: { // for initializing 3 hours check
        type: Date,
        default: Date.now
    },
    lastUpdated: { // check every 3 hours for issuing free points
        type: Date,
        default: Date.now
    }
}, {
    collection: 'profile',
    strict: 'throw'
});

var Profile = mongoose.model('Profile', profileSchema);

module.exports = Profile;

我也用我的模型更新了帖子,我的模型/架构是否与我的相关 db.insertions?

1 个答案:

答案 0 :(得分:1)

你的回答

var moment = require('moment');
var Profile = require('./model/profile.js');
var threeHours = 10800000;//3 hours in millisecs
Profile.findOne({ userName: "Rick" }, function (err, result) {
    var nowDate = new Date();
    var currentTime = moment(nowDate);
    var lastUpdated = moment(result.lastUpdated);
    var TotalDuration =  Math.abs(moment.duration(currentTime.diff(lastUpdated)));//value in millisec
var totalPoints = result.points.free + result.points.purchased;
// console.log(timeSinceLastUpdate)
if (TotalDuration >= threeHours) {
    // ... time to update score ... 
    if (totalPoints < 200) {
        result.free += 50; // issue 50 free points
        Profile.update(
            {
                userName: "Rick"
            },
            {
                $set: {
                    points: {
                        free: result.free
                    }
                }
            },
            function (err, res) {
                if (err) throw err;
                console.log(res)
            })
    } else {
        // do nothing, wait for next check  
    }
}
})

参考moment

安装时刻(npm install --save moment)