If I do a .insert on a collection in Cloud code, how can I get the _id value of the document I just inserted?
There seems to be a way of doing it in MongoDB generally, but I can't work out how to do it from the Spark interfaces
Cheers,
Fred
Best Answer
C
Customer Support
said
about 8 years ago
Hi Fred,
The object you are inserting into the Collection will be updated to include the document's Id, for example:
var col = Spark.runtimeCollection("testDB");
var docToInsert = {"test1":"1", "test2":"t"}; //Has no $oid
var insertedDoc = col.insert(docToInsert);
Spark.setScriptData("insertedDocId", docToInsert._id.$oid); //If the insertion was successful it should now have an $oid
The object you are inserting into the Collection will be updated to include the document's Id, for example:
var col = Spark.runtimeCollection("testDB");
var docToInsert = {"test1":"1", "test2":"t"}; //Has no $oid
var insertedDoc = col.insert(docToInsert);
Spark.setScriptData("insertedDocId", docToInsert._id.$oid); //If the insertion was successful it should now have an $oid
Has this changed? Insert seems to return true or false now.
Customer Support
said
over 5 years ago
Hi Barry,
The insert operation returns true or false depending on whether it has succeeded or not. It's the scriptData line that's returning the value, specifically "docToInsert._id.$oid".
Fred Pollard
If I do a .insert on a collection in Cloud code, how can I get the _id value of the document I just inserted?
There seems to be a way of doing it in MongoDB generally, but I can't work out how to do it from the Spark interfaces
Cheers,
Fred
Hi Fred,
The object you are inserting into the Collection will be updated to include the document's Id, for example:
Here's the response to running that script:
Shane
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstCustomer Support
Hi Fred,
The object you are inserting into the Collection will be updated to include the document's Id, for example:
Here's the response to running that script:
Shane
1 person likes this
Fred Pollard
Brilliant - will give this a try, thanks
Barry Walsh
Has this changed? Insert seems to return true or false now.
Customer Support
Hi Barry,
The insert operation returns true or false depending on whether it has succeeded or not. It's the scriptData line that's returning the value, specifically "docToInsert._id.$oid".
Regards,
Liam
1 person likes this
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics