New to Mongodb,trying to get _id after mongodb insert without a round trip.
$.ajax( { url: "https://api.mongolab.com/api/1/databases/xxx/collections/xx?apiKey=xxx",
data: JSON.stringify( [ { "x" : 2,"c1" : 34,"c2" : getUrlVars()["c2"]} ] ),
type: "POST",
contentType: "application/json" } );
Thanks
edit: Solved buy removing square bracers JSON.stringify( { "x" : 2,"c1" : 34,"c2" : getUrlVars()["c2"]} )
You can’t get the _id of the object without waiting for a response because the _id is generated on the server. You need to attach a success call to your request. The id is actually returned as a $oid member within an _id object.
This is assuming you are using $.ajax to make the request and the MongoLab REST API as documented at http://support.mongolab.com/entries/20433053-rest-api-for-mongodb
For example: