最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Import JSON with mongo script - Stack Overflow

programmeradmin2浏览0评论

I'm trying to write a mongo script to import a jsonArray from a JSON file. My script is in .js format and I execute it with load() command in mongo shell. Is it possible to do it with a mongo script?

I know I can use mongoimport instead. But I want to know a way to do it with a script.

The contents of my current script in which the import part is missing is given below..

var db = connect("localhost:27017/fypgui");
//Import json to "crimes" collection here 
var crimes = db.crimes.find();
while (crimes.hasNext()){
    var item = crimes.next();
    var year =(item.crime_date != null)?(new Date(item.crime_date)).getFullYear():null;
    db.crimes.update( {_id: item._id}, {$set: {crime_year: year}});
}

I'm trying to write a mongo script to import a jsonArray from a JSON file. My script is in .js format and I execute it with load() command in mongo shell. Is it possible to do it with a mongo script?

I know I can use mongoimport instead. But I want to know a way to do it with a script.

The contents of my current script in which the import part is missing is given below..

var db = connect("localhost:27017/fypgui");
//Import json to "crimes" collection here 
var crimes = db.crimes.find();
while (crimes.hasNext()){
    var item = crimes.next();
    var year =(item.crime_date != null)?(new Date(item.crime_date)).getFullYear():null;
    db.crimes.update( {_id: item._id}, {$set: {crime_year: year}});
}
Share Improve this question edited Dec 27, 2014 at 20:30 Sampath Liyanage asked Dec 27, 2014 at 20:04 Sampath LiyanageSampath Liyanage 4,8962 gold badges31 silver badges40 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 12

There is another answer to this question. Even though it's a bit old I am going to respond.

It is possible to do this with the mongo shell.

You can convert your JSON to valid JavaScript by prefixing it with var myData= then use the load() command to load the JavaScript. After the load() you will be able to access your data from within your mongo script via the myData object.

data.js

var myData=
[
        {
                "letter" : "A"
        },
        {
                "letter" : "B"
        },
        {
                "letter" : "C"
        }
]

read.js

#!/usr/bin/mongo --quiet

// read data

load('data.js');

// display letters

for (i in myData) {
  var doc  = myData[i];
  print(doc.letter);
}

For writing JSON is easiest to just load your result into a single object. Initialize the object at the beginning with var result={} and then use printjson() at the end to output. Use standard redirection to output the data to a file.

write.js

#!/usr/bin/mongo --quiet

var result=[];

// read data from collection etc...
for (var i=65; i<91; i++) {
  result.push({letter: String.fromCharCode(i)});
}

// output
print("var myData=");
printjson(result);

The shebang lines (#!) will work on a Unix type operating system (Linux or MacOs) they should also work on Windows with Cygwin.

It is possible to get file content as text using undocumented cat() function from the mongo shell:

var text = cat(filename);

If you are interested cat() and other undocumented utilities such as writeFile are defined in this file: shell_utils_extended.cpp

Having file's content you can modify it as you wish or directly pass it to JSON.parse to get JavaScript object:

jsObj = JSON.parse(text);

But be careful: unfortunately JSON.parse is not an equivalent of mongoimport tool in the sense of its JSON parsing abilities.

mongoimport is able to parse Mongo's extended JSON in canonical format. (Canonical format files are created by bsondump and mongodump for example. For more info on JSON formats see MongoDB extended JSON).

JSON.parse does not support canonical JSON format. It will read canonical format input and will return JavaScript object but extended data type info present in canonical format JSON will be ignored.

No, the mongo shell doesn't have the capability to read and write from files like a fully-fledged programming environment. Use mongoimport, or write the script in a language with an official driver. Node.js will have syntax very close to the mongo shell, although Node.js is an async/event-driven programming environment. Python/PyMongo will be similar and easy to learn if you don't want to deal with structuring the logic to use callbacks.

Hey I know this is not relevent but, every time I need to import some jsons to my mongo db I do some shity copy, paste and run, until I had enough!!! If you suffer the same I v written a tiny batch script that does that for me. Intrested?

https://github.com/aminjellali/batch/blob/master/mongoImporter.bat

@echo off
title = Mongo Data Base importing tool
goto :main

:import_collection
    echo importing %~2
    set file_name=%~2
    set removed_json=%file_name:.json=%
    mongoimport --db %~1 --collection %removed_json% --file %~2
goto :eof

:loop_over_files_in_current_dir
    for /f %%c in ('dir /b *.json') do call :import_collection %~1 %%c
goto :eof

:main
    IF [%1]==[] (
    ECHO FATAL ERROR: Please specify a data base name
    goto :eof
    ) ELSE (
    ECHO @author amin.jellali
    ECHO @email [email protected]
    echo starting import...
    call :loop_over_files_in_current_dir %~1
    echo import done...
    echo hope you enjoyed me
    )
goto :eof

I was looking for this for quite a while and the given anwers were either not working or just providing a workaround.

Luckily I found the answer.. You can use fs.readFileSync to read (json) files. For completeness sake here is my entire script:

const dbName = process.env.DB_NAME || 'test_db';
const dbUser = process.env.DB_USER || 'test_user';
const dbPassword = process.env.DB_PASSWORD || 'secret';

db.createUser({
  user: dbUser,
  pwd: dbPassword,
  roles: [
    {
      role: 'readWrite',
      db: dbName,
    },
  ],
});

// Create and seed collections:

const collections = [
  'some_collection',
  'another_collection',
];

for (let i = 0; i < collections.length; i++) {
  const collectionName = collections[i];
  db.createCollection(collectionName);

  let jsonFile;
  try {
    jsonFile = fs.readFileSync(`/docker-entrypoint-initdb.d/mongo.${collectionName}.json`, 'utf8');
  }
  catch (err) {
    console.log(`NOTE: mongo.${collectionName}.json not found! Skip seeding of ${collectionName} collection..`)
    continue;
  }

  const json = JSON.parse(jsonFile);

  // Parse the $oid ids that Mongo uses in exports, but cant use in insertMany:
  for (let i = json.length; i--;) {
    json[i]._id = ObjectId(json[i]._id.$oid);
  }

  db[collectionName].insertMany(json);
}
发布评论

评论列表(0)

  1. 暂无评论