Skip to main content

#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.

warning

If a document is inserted containing an _id that already exists in the user's database, a :::TRUST COMMUNICATION::: will be raised:

:::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.

Return Data

An object is returned upon inserting one or many documents into the database, providing extra details about whether it succeeded:

KeyInformation
nNumber of documents inserted
okIf the operation succeeded, == 1
opTimeObject 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

Example

function(context, args) {
return #db.i([ { type: "my_data", my_key: "foo" }, { type: "my_data", my_key: "bar" } ])
}