猫鼬更新不更新

Ger*_*int 2 mongoose mongodb node.js

我正在尝试更新具有当前日期/时间的特定 ID 的文档,但以下代码不会导致数据库更新并且没有错误。任何帮助都会很棒,谢谢。

架构:

var MerchantShema = new Schema({
    merchant_id: Number,
    merchant_aw_id: Number,
    merchant_name: String,
    merchant_url: String,
    merchant_image: String,
    product_feed: String,
    product_feed_updated: Date,
    created_at: {type: Date, default: Date.now},
    updated_at: {type: Date, default: Date.now}
});
Run Code Online (Sandbox Code Playgroud)

更新查询:

updateMerchantLastProductUpdate: function (mID) {
  now = new Date();      

  var query = { "merchant_aw_id" : mID };
  Merchants.update(query, { "product_feed_updated": now }, function (err) {
    if (err) return console.error(err);        
  })
}
Run Code Online (Sandbox Code Playgroud)

路线

  app.get('/queries', function (req, res) {
    queries.updateMerchantLastProductUpdate("2926");
  });
Run Code Online (Sandbox Code Playgroud)

示例文档

{
    "_id": {
        "$oid": "55997638e4b01f0391cb99aa"
    },
    "merchant_id": "0003",
    "merchant_aw_id": "2926",
    "merchant_name": "Multipower",
    "merchant_url": "www.multipower.com/uk/",
    "merchant_image": "",
    "product_feed": "aw",
    "product_feed_updated": "",
    "created_at": "",
    "updated_at": ""
}
Run Code Online (Sandbox Code Playgroud)

chr*_*dam 5

猫鼬模式中的字段需要一个数字,因此您需要使用查询中的方法merchant_aw_id来解析字符串以获取整数。parseInt()您还需要$set更新运算符(用指定值替换字段值来更新文档),以及{multi: true}如果设置为 true 则更新满足查询条件的多个文档的选项。如果设置为 false,则更新一份文档。默认值为 false:

updateMerchantLastProductUpdate: function (mID) {
    var now = new Date(),
        query = { "merchant_aw_id" : parseInt(mID) },
        update = {
            "$set": { "product_feed_updated": now } 
        },
        options = { "multi": true };

    Merchants.update(query, update, options, function (err) {
        if (err) return console.error(err);         
    })
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,但刚刚尝试过,我也遇到了同样的问题。数据库未更新/没有错误 (3认同)