So I do not know much about MongoDB
. I have RoboMongo
using which I connect to a MongoDB. What I need to do is this - there is a collection in that Mon
//on the same db
var cursor = db.collectionname.find();
while (cursor.hasNext()) {
var record = cursor.next();
db.new_collectionname.save(record);
}
You can add the --jsonArray
parameter / flag to your mongoexport
command, this exports the result as single json array.
Then just specify the --jsonArray
flag again when importing.
Or remove the starting and ending array brackets [] in the file, then your modified & exported file will import with the mongoimport
command without the --jsonArray
flag.
More on Export here: https://docs.mongodb.org/manual/reference/program/mongoexport/#cmdoption--jsonArray
Import here: https://docs.mongodb.org/manual/reference/program/mongoimport/#cmdoption--jsonArray
If you want to use mongoimport, you'll want to export this way:
db.getCollection('tables')
.find({_id: 'q3hrnnoKu2mnCL7kE'})
.forEach(function(x){printjsononeline(x)});
Don't run this command on shell, enter this script at a command prompt with your database name, collection name, and file name, all replacing the placeholders..
mongoexport --db (Database name) --collection (Collection Name) --out (File name).json
It works for me.
A Quick and dirty way: Just write your query as db.getCollection('collection').find({}).toArray()
and right click Copy JSON
. Paste the data in the editor of your choice.
Solution:
mongoexport --db test --collection traffic --out traffic.json<br><br>
Where:
database -> mock-server
collection name -> api_defs
output file name -> childChoreRequest.json