Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用mongoose更新mongodb中的文档_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript 使用mongoose更新mongodb中的文档

Javascript 使用mongoose更新mongodb中的文档,javascript,node.js,mongodb,mongoose,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 resul

我正在尝试使用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  

        }

    })
但是实际的数据库没有得到更新。。请纠正我的错误

任何帮助都将不胜感激


编辑: 这是我的模型

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.插入?

您的答案

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  
    }
}
})
var-moment=require('moment');
var Profile=require('./model/Profile.js');
var三小时=10800000//3小时毫秒
findOne({userName:“Rick”},函数(err,result){
var nowDate=新日期();
var currentTime=时刻(nowDate);
var lastUpdated=时刻(result.lastUpdated);
var TotalDuration=Math.abs(矩.时长(currentTime.diff(LastUpdate));//以毫秒为单位的值
var totalPoints=result.points.free+result.points.purchased;
//console.log(timeSinceLastUpdate)
如果(总持续时间>=三小时){
//…是时候更新分数了。。。
如果(总点数<200){
result.free+=50;//获得50个自由积分
Profile.update(
{
用户名:“瑞克”
},
{
$set:{
要点:{
免费的,免费的
}
}
},
函数(err、res){
如果(错误)抛出错误;
console.log(res)
})
}否则{
//什么也不做,等待下一次检查
}
}
})
提及


install moment by(npm install--save moment)

您使用的是mongodb客户端(MongoClient)或mongoose上述代码语法不是mongoose,而是您不插入的mongodb客户端(MongoClient)updating@RatanUdayKumar我用的是猫鼬,如何更新文档?您的代码不是Mongoose当我用您的答案更新代码时,我得到
“$set”:{
丢失)参数列表后
错误我再次更改了代码,它没有更新任何内容它没有存储在数据库中它现在更新之后,问题是异步函数,谢谢你今天的帮助,你节省了我大量的时间祝你愉快!
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  
    }
}
})