0

I have a MongoDB schema that looks like this:

const batchSchema = mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  time: {type: [Number]},
  tmp: {type: [Number]},
  hum: {type: [Number]},
  co2: {type: [Number]},
  coolRelay: {type: [Boolean]},
  humRelay: {type: [Boolean]},
  fanRelay: {type: [Boolean]},
  lightRelay: {type: [Boolean]},
});

My goal is to have a csv file I can import to excel for use in creating charts, graphs, and other visuals based on the data.

Using Studio 3T(https://robomongo.org/), this doesn't seem to be possible unless I'm just not understanding the settings. Doesn't really matter if the data is organized horizontally or vertically. As long as each is on it own column/row, excluding the _id field. 3T seems to be only capable of placing them all along the same row.

I just tried using mongo's native cli csv export tool but found that didn't seem to come with my release. I'm running version 3.6 on openSuse, arm64, on a raspberry pi.

mongoexport as a command was not found, and when I ran the suggested cnf mongoexport to find a package containing it, nothing was returned either.

I know that I can export one file, per array using 3T, then cut/paste them together, however I hadn't planned on paying the annual license for 3T and was only using the free trial to get a more usable gui while in development. Was hoping to be able to write predefined bash script that could be reused repeatedly to export each "batch" document as it was completed.

Is there something I am missing about mongoexport or is this a limitation of the aarch64 release? I really don't want to have to go back, learn, and re-write everything for postgreSQL at this point. I mean, I will if I have to, but wanted to check in with you all first.

  • Welcome to the site, what you want import of .csv data into MongoDB or want to export MongoDB collection data in .csv format. – Md Haidar Ali Khan Mar 31 at 5:16
0

MongoDB CSV Export: One Array/Column & Array Index on rows, or vice versa

I will suggest to you, if you are unable to import or export collections data from MongoDB CLI. Then you install the Compass, From MongoDB Compass you can easily import and export the collection data. As per MongoDB documentation here MongoDB Compass works with MongoDB Server version 3.0 and higher, both Enterprise and Community editions. It runs on 64-bit versions of Mac OS X, Windows, Ubuntu 14.04+ and RHEL 7+.

Note: As of MongoDB Server version 3.6, MongoDB packages a platform-specific Compass installation script with MongoDB Server downloads.

For example

I have a database test and collection articles having the records such as

> use test
switched to db test
> show collections
alpha
articles
books
oplog
people
sales
sample
team
users

> db.articles.find().pretty();
{ "_id" : 1, "subject" : "coffee", "author" : "xyz", "views" : 50 }
{ "_id" : 2, "subject" : "Coffee Shopping", "author" : "efg", "views" : 5 }
{ "_id" : 3, "subject" : "Baking a cake", "author" : "abc", "views" : 90 }
{ "_id" : 4, "subject" : "baking", "author" : "xyz", "views" : 100 }
{
        "_id" : 5,
        "subject" : "Café Con Leche",
        "author" : "abc",
        "views" : 200
}
{ "_id" : 6, "subject" : "Сырники", "author" : "jkl", "views" : 80 }
{
        "_id" : 7,
        "subject" : "coffee and cream",
        "author" : "efg",
        "views" : 10
}
{ "_id" : 8, "subject" : "Cafe con Leche", "author" : "xyz", "views" : 10 }
>

The collections records are appearing in MongoDB Compass in Table format such as

enter image description here

Now I am going to export the collections records through MongoDB compass tool. After collecting with your host computer (either local or remote) with MongoDB compass tool . Select the database (here I have taken test database) then click on the collections name (here I have been taken articles) then click on the Collection menu tab. It will appears like that

enter image description here

According to your desire you click on the option, As here I want to export the collection data in .csv format then I will click on export command and then the screen will be appears like that

enter image description here

Then browse the file location as per your desire and export the collection records in .csv format.

After exporting the collections records from the compass in .csv format the appearance in .csv of data will be look like as follows:

enter image description here

  • Okay, this makes a little more sense. Every object becomes its own row, and each field within that object is a column. For my purposes, I would need an array of objects. Because it would look like this Schema({ _id: mongoose.Schema.Types.ObjectId, lifeSpan: [{ tmp: {type: Number}, co2: {type: Number}, hum:{type: Number} }] }), this then throws all the sub-documents in to an array inside a single cell. Is there a way to pull all the sub-documents out of the array so that they then will occupy their own row? – Eru Mar 31 at 16:46
0

After some more digging I found this: https://stackoverflow.com/questions/26378769/mongodb-mongoexport-all-objects-in-nested-array

Seems like I'm going to have to do some learning on how to use $unwind & $out to get the desired result I'm looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.