1

We are trying to post json string from c# to mongodb stored javascript using driver ver 1.8

Here is my js function :

function addCorporate (json) {
    db.Corporate.save(json);

   /* obj = JSON.parse(json);
    if(obj.corporateName) {        
        db.Corporate.save(obj);
    }
    */
}

Note: We tried to run it with commented code as well

C# code :

var CorporateData="{\"corporateName\":\"a\"}";

MongoClient client = new MongoClient(hostedWebConnectionString);
            MongoServer server = client.GetServer();
            MongoDatabase db = server.GetDatabase("myDb");

            BsonValue bv = db.Eval("addCorporate", CorporateData);
            BsonValue bv1 = db.Eval(bv.AsBsonJavaScript.Code, CorporateData);

The above code gives error as follows:

Command '$eval' failed: exception: Error: can't save a null
at Error (<anonymous>)

at DBCollection.save (src/mongo/shell/collection.js:486:15)

at _funcs1 (_funcs1:2:18) at src/mongo/shell/collection.js:486 (response: { "errmsg" : "exception: Error: can't save a null\n    at Error (<anonymous>)\n    at DBCollection.save (src/mongo/shell/collection.js:486:15)\n    at _funcs1 (_funcs1:2:18) at src/mongo/shell/collection.js:486", "code" : 16722, "ok" : 0.0 })

If we uncommnet the lines in js function and comment the 1st line then we get following error:

"Command '$eval' failed: exception: SyntaxError: Unexpected token u
at Object.parse (native)

at _funcs1 (_funcs1:4:16) (response: { "errmsg" : "exception: SyntaxError: Unexpected token    at Object.parse (native)    at _funcs1 (_funcs1:4:16)", "code" : 16722, "ok" : 0.0 })            

Note: We also tried to parse it as BsonValue i.e

var bsonString = (BsonValue)CorporateData;

but still it gives error.

Please suggest any solutions.

Thanks in advance.

Jatin
  • 11
  • 4

1 Answers1

0

If you are using mongocsharpdriver, then you have to change your object CorporateData to BsonDocument before saving, check out the following code snippet(I am currently using this in my project),

var CorporateData="{\"corporateName\":\"a\"}";
var yourCollectionName="CorporateInformation";

MongoClient client = new MongoClient(hostedWebConnectionString);
MongoServer server = client.GetServer();
MongoDatabase db = server.GetDatabase("myDb");
var document = BsonSerializer.Deserialize<BsonDocument>(CorporateData);
var collection = mongoDB.GetCollection<BsonDocument>(yourCollectionName);
collection.Insert(document);

Hope this helps :)

UPDATED: To use javascript function defined in mongodb. IT IS NOT RECOMMENDED BECAUSE IT MEANS THAT YOU ARE SHIFTING YOUR BUSSINES LOGIC TO DB. OVER THE TIME IT WILL BE DIFFICULT TO MANAGE.

You have not provided function name to your javascript function, change it to following,

function addCorprate(json) {
    db.Corporate.save(json);

   /* obj = JSON.parse(json);
    if(obj.corporateName) {        
        db.Corporate.save(obj);
    }
    */
}

and your code should work as it is.

AbdulRahman Ansari
  • 3,007
  • 1
  • 21
  • 29
  • This doesn't uses stored javascript , I want to use store js function and insert it. – Jatin Mar 10 '16 at 06:57
  • Sorry forgot to write name of function over here , but i have given name in my code and updated 1st post over here as well. – Jatin Mar 10 '16 at 07:37
  • Yea I know we are shifting the logic to DB as we have skilled ppl with mongodb skills and would prefer them to handle this. – Jatin Mar 10 '16 at 10:01