0

now that I am able to insert documents to my local Mongo, make some queries and display the resulting documents into a LiveCode Data Grid, I considered continue with some validation, begining with the simple verification of duplicate _ids. I saw that as espected, Mongo refuses to insert documents with duplicate ids, the problem is how to detect this situation.

I thought I have to somehow I got to check getLastError or something Mongo reports from the insert operation success.

To do this, I add some code to my Insert button:

put "db.runCommand( { getLastError:" &  "'" & 1 & "' ," &  "w : " &  "'" & 1 & "' })" into dbErr
put shell ( theMongo & theDB && "--eval" && quote & dbErr & quote) into dbResultado

Then I pass dbResultado to the function JSONToArray(tJSON), but I get error

button "Insert Doc": execution error at line n/a (External handler: exception) near "could not decode JSON: invalid token near 'object'"    

I think dbResultado is a JSON because when I put it in the message, it gets:

MongoDB shell version: 2.2.7
connecting to: localhost:27017/BatGar
[object bson_object]

Maybe I am trying something that I shouldn't and there is other way of getting the error from Mongo to later operate based on it.

Mark
  • 2,380
  • 11
  • 29
  • 49
Javier
  • 75
  • 5
  • Try wrapping the result of the getLastError call with printjson(). The result returned to you here is a bson object and has to be converted to json to be human readable. – James Wahlin Apr 08 '14 at 15:41
  • It would also be worth your while reading the following on scripting with the mongo shell: http://docs.mongodb.org/manual/tutorial/write-scripts-for-the-mongo-shell/ – James Wahlin Apr 08 '14 at 15:42
  • I don't think you are going to get a good reply to this question. E.g., it looks like you can't execute db.runCommand() this way. A good start would be to post a larger part of your scripts. Keep in mind that db.runCommand needs a context. It doesn't work if it is the only command in a script counting only one line (AFAIK). – Mark Apr 10 '14 at 12:39
  • Thank you Mark, what I did is applying James W. suggestion, with this I was able to tell if the insert operation succeeded. My next task is trying to remove documents. With this I will almost finish all the CRUD (basic) operations. – Javier Apr 13 '14 at 02:26

0 Answers0