nodejs mongo代码不适用于新的mongo版本

时间:2015-02-23 14:04:51

标签: node.js mongodb

以下代码适用于mongo 2.4.9,但不适用于最新版本(2.6.7)。

我知道键中有一个点问题,但我发现很难相信mongo像这样打破了向后兼容性

我的问题是,我的客户能否以某种方式调整mongo 2.6.7以使其工作?还是在更改代码或降级mongo之间做出选择?

var MongoClient = require('mongodb').MongoClient
    , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    console.log("Connected correctly to server");

    var blueprints = db.collection('blueprints');
    blueprints.insert({}, function( err, insertedList ){
        var insertedListId = insertedList._id;
        insertedList =  {"userId":"54eae2f9e9409b4c464f1499","nodesToSave":[{"width":140,"height":100,"id":1,"name":"Guy","type":"Root","uiType":"Node","x":340,"y":60,"infoData":{"properties":{},"interfaces":{"cloudify.interfaces.lifecycle":{"create":{},"configure":{},"start":{},"stop":{},"delete":{}},"cloudify.interfaces.validation":{"creation":{},"deletion":{}},"cloudify.interfaces.monitoring":{"start":{},"stop":{}}}},"templateData":{"type":"cloudify.nodes.Root"},"markedContainedIn":false}],"connectorsToSave":[]};
        insertedList._id = insertedListId;
        blueprints.update({ '_id' : insertedList._id} , insertedList , { upsert: true },function(){
            console.log('after update', arguments);
        });
        console.log('after insert',arguments);
    });

});

1 个答案:

答案 0 :(得分:0)

insertedList是一个滚动的野兽。仅供参考,如果你去JSONLint并验证JSON,只要它没有被完全破坏,它就会很好地为你格式化,即使它不是有效的JSON(例如它适用于mongo shell BSON语法) ):

{
    "userId": "54eae2f9e9409b4c464f1499",
    "nodesToSave": [
        {
            "width": 140,
            "height": 100,
            "id": 1,
            "name": "Guy",
            "type": "Root",
            "uiType": "Node",
            "x": 340,
            "y": 60,
            "infoData": {
                "properties": {},
                "interfaces": {
                    "cloudify.interfaces.lifecycle": {
                        "create": {},
                        "configure": {},
                        "start": {},
                        "stop": {},
                        "delete": {}
                    },
                    "cloudify.interfaces.validation": {
                        "creation": {},
                        "deletion": {}
                    },
                    "cloudify.interfaces.monitoring": {
                        "start": {},
                        "stop": {}
                    }
                }
            },
            "templateData": {
                "type": "cloudify.nodes.Root"
            },
            "markedContainedIn": false
        }
    ],
    "connectorsToSave": []
}

问题出在cloudify.interfaces.[lifecycle, validation, monitoring]上。这些是非法的字段名称 - .'字段名称中不允许使用s。 2.6严格执行此更新。

作为升级到2.6的一部分,有一个函数db.upgradeCheckAllDbs()可以找到这样的问题。除了删除这些字段之外,没有任何修复。