#db.i(..)
Add new document(s) to the database.
Similar to a MongoDB db.collection.insert(), with some syntax restrictions
Syntax
#db.i(documents)
Arguments
documents
An object or array of objects, representing documents to be inserted
_id
If not specified, #db.i(..) will add an _id key-value pair to your document on insertion, containing an ObjectId. This is to ensure that there is at least 1 unique property to each document
If an _id is included in the inserted document, that _id will be used. This has unique advantages in hackmud, because the _id field is indexed in user database collections, resulting in significantly faster querying times from other database functions.
If a document is inserted containing an _id that already exists in the user's database, an error will be thrown:
:::TRUST COMMUNICATION::: E11000 duplicate key error collection: hackmud*production.[user] index: \_id* dup key: { : [id value] } (11000)
Any other documents inserted alongside the errant document will still be inserted.
You can catch these errors with try
/catch
if necessary. If you do not manually specify the _id
fields of inserted documents, these errors should never happen.
Return Data
When inserting one document, an array of length 1 is returned, containing a single object, providing extra details about whether the insert succeeded:
Key | Information |
---|---|
n | Number of documents inserted |
ok | If the operation succeeded, == 1 |
opTime | Object that contains a property 't' -- time of insertion operation in millieconds |
Other values are returned, but provide no additional usable data due to undefined conversions
When inserting an array of documents, a single object (not an array) is returned, containing:
Key | Information |
---|---|
n | Number of documents inserted |
n_inserted | Number of documents inserted... again? |
inserted_ids | Array of _id s of the inserted documents |
Example
function(context, args) {
return #db.i([ { type: "my_data", my_key: "foo" }, { type: "my_data", my_key: "bar" } ])
}