Search code examples
mongodbcsvmongoexport

MongoExport error - cannot unmarshal string


I have a MongoDB collection with the following documents. Some of the documents have 1 field and some have 2. I am interested in exporting only those that I have the field "productid". I am using the query below but getting the error: "cannot unmarshal string into GO value of type map[string] interface {}".

The document looks like this:

[
  {
     "id" : 1,
  },
  {
     "id" : 2,
  },
  {
     "id" : 3
     "Product Info":
      {
         "ProductName" : "test"
      }
  }
]

The MognoExport command I am using is as follows: mongoexport --username x --password x --host x --db mydb --collection mycol --query '{"Product Info.ProductName":{"$exists":true}}' --type=csv --fields id,productid --out "c:\myfile.csv"


Solution

  • I fixed this issue by updating my script to:

    mongoexport --username x --password x --host x --db mydb --collection mycol --query "{ 'Product Info.ProductName':{$exists:true}}" --type=csv --fields id,productid --out "c:\myfile.csv"