-3

I have looked through other posts and I have checked that I'm not using the wrong quotes, I feel like it's right in front of my face but can't see it! Trying to copy and paste data from local mongoDB to mLab but it's not working here's my JSON:

{
    "_id": ObjectId("59e3bf8aa593df919c1c2c87"),
    "key": "core",
    "name": "Core",
    "__v": 0
}

{
    "_id": ObjectId("59e3c04ca593df919c1c2c89"),
    "key": "total-body",
    "name": "Total Body",
    "__v": 0
}

{
    "_id": ObjectId("59e3c054a593df919c1c2c8a"),
    "key": "lower-body",
    "name": "Lower Body",
    "__v": 0
}

{
    "_id": ObjectId("59e3c05fa593df919c1c2c8b"),
    "key": "upper-body",
    "name": "Upper Body",
    "__v": 0
}

JSONLint gives me back this error:

Error: Parse error on line 2:
{   "_id": ObjectId("59e3bf8aa5
---------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'
RioTz
  • 3
  • 1
  • 4
  • There are two or three problems: 1. `ObjectId("...")` is obviously not JSON. 2. In JSON, there must be **one** top-level value. You have four. 3. If those values were in a container (say, an array), you'd need `,` between them. – T.J. Crowder Mar 21 '18 at 08:36
  • That is not a json, that is `Bson` which is mongodb type to keep data – Efe Mar 21 '18 at 08:37
  • @Efe: It may be a Mongo-specific format, but it's not BSON. BSON is binary (that's the "B"). The above is text. – T.J. Crowder Mar 21 '18 at 08:38
  • My bad pretty new to databases Still I have to problem transferring local 'Bson' to mLab it seems it's not directly transferable? – RioTz Mar 21 '18 at 08:40
  • @T.J.Crowder Yes you are right `bson` is called as binary-encoded format which extends JSON with additional data types. So anyway, i wanted to meaning this. – Efe Mar 21 '18 at 08:41
  • @RioTz you can import data easily with [mongorestore](https://docs.mongodb.com/manual/reference/program/mongorestore/) command. – Efe Mar 21 '18 at 08:43
  • @Efe Not going to lie I spent 14 hours yesterday trying to do it on the command line.. so today I'm manually doing it haha.. I waste a whole day of my life on this :') – RioTz Mar 21 '18 at 08:46
  • Glad you got the data imported on Robo3T. As an FYI, the reason you got this error on the mLab website is because the data editor on mLab uses strict MongoDB Extended JSON (https://docs.mongodb.com/manual/reference/mongodb-extended-json/). In strict MongoDB Extended JSON `ObjectId("")` needs to be `{ "$oid": "" }`. – tfogo Mar 27 '18 at 18:46

1 Answers1

1

Give the command-line one more try. This worked for me:

$ mongo ds999999.mlab.com:17688/sandbox -u dbuser -p password
MongoDB shell version: 3.2.11
connecting to: ds999999.mlab.com:17688/sandbox
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user

rs-ds999999:PRIMARY> db.foo.insertOne({"_id": ObjectId("59e3c05fa593df919c1c2c8b"), "key": "upper-body", "name": "Upper Body", "__v": 0})
{
    "acknowledged" : true,
    "insertedId" : ObjectId("59e3c05fa593df919c1c2c8b")
}

Your documents look like valid BSON.

Or, insert a bunch in one command:

rs-ds999999:PRIMARY> db.foo.insertMany([{ "_id": ObjectId("59e3bf8aa593df919c1c2c87"), "key": "core", "name": "Core", "__v": 0}, { "_id": ObjectId("59e3c04ca593df919c1c2c89"), "key": "total-body", "name": "Total Body", "__v": 0}, { "_id": ObjectId("59e3c054a593df919c1c2c8a"),  "key": "lower-body",  "name": "Lower Body", "__v": 0}])
{
    "acknowledged" : true,
    "insertedIds" : [
    ObjectId("59e3bf8aa593df919c1c2c87"),
    ObjectId("59e3c04ca593df919c1c2c89"),
    ObjectId("59e3c054a593df919c1c2c8a")
    ]
}
craigcaulfield
  • 3,381
  • 10
  • 32
  • 40
  • I'll give a summary of my problem: I'm deploying a KeystoneJS app to Heroku, I managed to get it uploaded but when it connected the database it seemed only the collection names have been created (but no data)? So I created a new user on mLab and when I tried connecting with the first command in mongo.exe (with the created user's username and pass) it gives me this: '2018-03-21T10:31:26.364+0000 E QUERY [thread1] SyntaxError: missing ; before statement @(shell):1:6'?? – RioTz Mar 21 '18 at 10:39
  • $ mongo ds999999.mlab.com:17688/sandbox -u dbuser -p password but with my database values – RioTz Mar 21 '18 at 11:14
  • There are a couple of things to check: 1. the `mongo ds99999...` command should be run from your Windows/Linux/Mac command line, not the mongo shell; 2. does your database or collection start with a number or an underscore? You may need to double-quote the names if they do. – craigcaulfield Mar 21 '18 at 11:34
  • I also tried it there but get this back: " 'mongo' is not recognized as an internal or external command, operable program or batch file." as for the collection names they just start with letters? the only underscore is the one infront of "_id" in the records. – RioTz Mar 21 '18 at 11:39
  • It looks like you don't have mongo installed locally or it's not on your PATH. I assume when you do `mongo --version` from your operating system command line, you get an error? – craigcaulfield Mar 21 '18 at 11:44
  • Well that's strange because my app won't load locally unless i have mongod running and I can access the collections through RoboT3 so it's on my system.. why can't the comandline find it?? how do I change its path – RioTz Mar 21 '18 at 11:50
  • The easiest thing for now is to navigate to where mongo has been installed on your system, `cd` to the `bin` directory below that, and run your `mongo ds99999...` command from there. Setting mongo on your PATH more permanently depends on your operating system. – craigcaulfield Mar 21 '18 at 12:03
  • Or, if you're in Robo 3T with a connection to mLab, just run the `db.foo.insert...` commands from the mongo shell the application provides. – craigcaulfield Mar 21 '18 at 12:09
  • Thank you I didn't know I could use Robo 3T to do that, finally done it THANK YOU! – RioTz Mar 21 '18 at 12:37