0

I have a file which has key as IP "10.11.190.14:cgi6web-1": . When I try to insert the file in mongoDB, I get the following error:

Error: key 10.11.190.14:cgi6-web-1 must not contain '.' at Error (unknown source).

I dont have permissions to change the file , is there any work around to write the file to mongoDB with IP as key.

My Code is as follows:

var MongoClient = require('./lib/mongodb').MongoClient;
var file = require (__dirname + '/functions.json');

MongoClient.connect('mongodb://phx8b03c-fb1d',
    function (err, client) {
        if (err) throw err;

        client.createCollection('lbTopology' , function (err, collection) {
            if (err) throw err;

            collection.insert(file, 'lbTopology' , function (err) {
                if (err) throw err;

                client.close(function (err) {
                    if (err) throw err;

                    console.log('done');
                });
            });
        });
    })

Can someone please help me with this?

7
  • Replace '.' with '_' in file keys before trying to insert? Commented Aug 8, 2013 at 5:38
  • How do I do that ? Not very good in JS Commented Aug 8, 2013 at 5:39
  • 2
    Can they be changed from keys to values in the file? Rather than { "10.11.190.14:cgi6web-1": ... }, maybe { "key": "10.11.190.14:cgi6web-1", ... }? It would probably make querying simpler later: collection.find({ key: '10.11.190.14:cgi6web-1' }, ...). Commented Aug 8, 2013 at 5:49
  • I don't have permission to edit file, but just read it and write it to MONGODB Commented Aug 8, 2013 at 5:54
  • could you post the json file here? You may redact sensitive fields but we need the structure to get a sense of how to transform the json file before inserting to MongoDB. Commented Aug 8, 2013 at 6:01

1 Answer 1

3

You could possibly change "Pools" as it is in the file to an Array, moving the key so it's a property of each inner Object:

{
    "Pools": [
        {
            "_key": "10.2.90.83: cgi5-web-1-80",
            "tags": "dc=PHXodbPool=cgi5-web-1-80",
            "oname": "10.2.90.83: cgi5-web-1-80",
            "pname": "syi-web",
            "oid_suffix": [
                19
            ]
        },
        // ...
    ]
}

You can use Object.keys() and .map() to accomplish this:

var file = require (__dirname + '/functions.json');

file.Pools = Object.keys(file.Pools).map(function (key) {
    var pool = file.Pools[key];
    pool._key = key;
    return pool;
});

[Edit] LBs will be a bit more interesting as each object has a pnames that needs the same treatment. Also, since each item under pnames is a Number, you'll have to create an Object to hold it with its key.

file.LBs = Object.keys(file.LBs).map(function (key) {
    var lb = file.LBs[key];
    lb._key = key;

    lb.pnames = Object.keys(lb.pnames).map(function (pkey) {
        return { key: pkey, value: lb.pnames[pkey] };
    });

    return lb;
});
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Jonathan. So instead of using collection.insert(file, 'lbTopology' , function (err) { if (err) throw err; I need to use collection.insert(pool, 'lbTopology' , function (err) { if (err) throw err; ??
@user2325703 This should allow you to still .insert(file, ...). It just alters the Pools property before that so Mongo will be more accepting of it.
@JonathanLonowski Wouldn't this incur a bit overhead while saving, indexing etc ?
@S.D. Well, some, I'm sure. I don't know enough about profiling Mongo to really answer. Though, I wouldn't think this would cause anymore overhead than was attempted with the original object.
@ Jonathan. Itworked ..Thanks a lot. I have 1 more JSON file. "LBs": { "10.11.190.14": { "tags": "lbtype=netscaler-shared-primary host=sjclb99-map00.sjc.com", "oname": "10.11.190.14", "pnames": { "10.11.190.14:cgi6-web-1": 1, "10.11.190.14:icgi6-web-1": 1 }, Since pnames also have Key ,value. I tried this function file.LBs.pnames = Object.keys(file.LBs.pnames).map(function (key) { var lbPname = file.LBs.pnames[key]; lbPname._key = key; return lbPname; }); but dint work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.