【问题标题】:update a document in mongodb using mongoose使用 mongoose 更新 mongodb 中的文档
【发布时间】:2018-06-13 13:50:56
【问题描述】:

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

【问题讨论】:

  • 你使用的是mongodb客户端(MongoClient)或者mongoose
  • 以上代码语法不是mongoose,而是mongodb客户端(MongoClient)
  • 您正在插入而不是更新
  • @RatanUdayKumar 我正在使用猫鼬,如何更新文档?
  • 你的代码不是猫鼬

标签: javascript node.js mongodb mongoose


【解决方案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)

【讨论】:

  • 当我用你的答案更新我的代码时,我得到"$set": {missing ) after argument listerrors
  • 我再次更改了代码,它没有更新任何内容
  • 它没有存储在数据库中
  • 它现在正在更新,问题在于异步功能,感谢您今天的帮助,您节省了我很多时间
  • 祝你有美好的一天!
猜你喜欢
  • 2017-01-06
  • 1970-01-01
  • 2015-10-04
  • 2016-10-15
  • 1970-01-01
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
  • 2017-09-22
相关资源
最近更新 更多