node mongodb不保留零填充数据

时间:2014-08-04 14:30:45

标签: mongodb node-mongodb-native

我遵循本指南作为保存分析数据的基础: pre-aggregated-reports

我的代码生成了一个更新对象{},如下所示(实际示例):

{ '$inc': {},
  '$set':
   { 'hourly.9': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'minute.9.51': { total: 0, global: 0 },
     'minute.9.52': { total: 0, global: 0 },
     'minute.9.53': { total: 0, global: 0 },
     'minute.9.54': { total: 0, global: 0 },
     'minute.9.55': { total: 0, global: 0 },
     'minute.9.56': { total: 0, global: 0 },
     'minute.9.57': { total: 0, global: 0 },
     'minute.9.58': { total: 0, global: 0 },
     'minute.9.59': { total: 0, global: 0 },
     'hourly.10': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'hourly.11': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'hourly.12': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'hourly.13': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'hourly.14': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'hourly.15': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'hourly.16': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'hourly.17': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'hourly.18': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'hourly.19': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'hourly.20': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'hourly.21': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'hourly.22': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] },
     'hourly.23': { total: 0, global: 0, trends: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] } },
  '$addToSet': {}
}

更新代码如下所示:

daily.update(query, update, {upsert:true, safe:true}, function(err, result) {
    if(err) {
        console.log("error writing update. " + err);
        throw err;
    }
});

查询是一个简单的对象{' _id':someid},更新是上面列出的对象。创建了文档,但没有存储零填充字段。

{
    "_id" : "20140804/example.com/13567219",
    "metadata" : {
        "date" : ISODate("2014-08-04T04:00:00Z"),
        "host" : "example.com",
        "id" : "13567219"
    }
}



但是,如果我在mongo控制台中运行相同的upsert,则会创建/更新所有填充零的字段:

    "13" : {
        "total" : 0,
        "global" : 0,
        "trends" : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    },
    "14" : {
        "total" : 0,
        "global" : 0,
        "trends" : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    },
    ...
    ...
    "22" : {
        "total" : 0,
        "global" : 0,
        "trends" : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    },
    "23" : {
        "total" : 0,
        "global" : 0,
        "trends" : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    },
    "9" : {
        "total" : 0,
        "global" : 0,
        "trends" : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    }
},
"metadata" : {
    "date" : ISODate("2014-08-04T04:00:00Z"),
    "host" : "example.com",
    "id" : "13560169"
},
"minute" : {
    "9" : {
        "51" : {
                "total" : 0,
                "global" : 0
        },
        "52" : {
                "total" : 0,
                "global" : 0
        },
        "53" : {
                "total" : 0,
                "global" : 0
        },
        "54" : {
                "total" : 0,
                "global" : 0
        },
        "55" : {
                "total" : 0,
                "global" : 0
        },
        "56" : {
                "total" : 0,
                "global" : 0
        },
        "57" : {
                "total" : 0,
                "global" : 0
        },
        "58" : {
                "total" : 0,
                "global" : 0
        },
        "59" : {
                "total" : 0,
                "global" : 0
        }
    }
}



是否存在我在本机驱动程序上缺少的设置,以强制填充数据?

1 个答案:

答案 0 :(得分:0)

Mongo版本或本机驱动程序有/没有问题。问题是PLBKAC - 问题在于键盘和椅子之间。

在我的代码中(在节点上运行)我在文档查找之外进行了更新。因此,在从DB返回文档之前进行了更新...所有可爱的异步善良。

在文档查找中移动更新方法解决了更新问题,而不是预先分配文档。

我有这个:

...
... code that updates the update object with $inc's, and $addToSets...
...

daily.find({'_id' : id}, {}, {'limit': 1}).toArray(function(err, doc) {
    ...
    ...
});

daily.update(query, update, {upsert:true, safe:true}, function(err, result) {
    if(err) {
        console.log("error writing update. " + err);
        throw err;
    }
});

并将daily.update移到daily.find ...

daily.find({'_id' : id}, {}, {'limit': 1}).toArray(function(err, doc) {
    ...
    ...

    daily.update(query, update, {upsert:true, safe:true}, function(err, result) {
        if(err) {
            console.log("error writing update. " + err);
            throw err;
        }
    });
});

所以未来的人,确保你对更新对象的所有编辑都是异步安全的:)