"Operation passed in cannot be an array" compass error - but JSON isn't an array
Asked Answered
H

3

5

Trying to import simple data (see below), gets me an error "Operation passed in cannot be an array" in Compass 1.29.6

This error makes no sense to me, as the outer object is not an array. Sure, the first object contains an array, but how is this not importable, since it is valid JSON? What is it expecting?

I have checked the documentation for Compass, perused other similar errors, no answers are obvious or relevant enough to help me.

{
   "Name": "root",
   "ID": 0,
   "Children": [{
    "Name": "Chocolates",
    "ID": 1,
    "ParentID": 0
   }]
}
Hoff answered 12/6, 2022 at 7:44 Comment(3)
No problem to insert it to Robo-3TTobitobiah
Try: db.getCollection(<name>).insert({ "Name": "root", "ID": 0, "Children": [{ "Name": "Chocolates", "ID": 1, "ParentID": 0 }] })Tobitobiah
So Compass and MongoDB require imported JSON data to be arrays instead of objects? The standard JSON empty object is {}, so it would be input as [{}] or as []?Nomen
M
7

I am a bit late but try to wrap your object into array like

[{
   "Name": "root",
   "ID": 0,
   "Children": [{
    "Name": "Chocolates",
    "ID": 1,
    "ParentID": 0
   }]
}]

it will work

Messuage answered 8/9, 2022 at 7:20 Comment(1)
Hey thanks. This saved me. It looks like the data I am getting from my sources is in pure JSON but what compass expects is a list of documents. So for my data I had to remove the outside wrapper object on my data but leave the list of objects inside it so that the "outermost structure" is a list.Goings
L
1

MongoDB Compass accepts documents in unformatted json structure, I used http://json.dylansweb.com/ to un-format the json and it worked fine in Compass.

Ligan answered 2/8, 2022 at 16:51 Comment(1)
Compass processed my formatted JSON just fine. Formatted vs. unformatted doesn't really make sense, since whitespace is irrelevant in JSON.Goings
D
-1

This has been an open issue on github for over 4 years now with no real solution from compass. Every suggestion is pretty hacky and the only real solutions seem to be to not use Compass https://github.com/mongo-express/mongo-express/issues/433

Devorahdevore answered 14/7, 2022 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.