insertOne

Updated at:

Adds a record to a collection.

Method definition

insertOne(doc: object): Promise<MongoResult>

Request parameters

Field name

Type

Required

Description

doc

Object

Yes

A single document record.

Note
  • The _id field is reserved by the system. Do not specify a value for this field when you insert data. The system automatically assigns a value. If you must specify a value for this field, it must be in the ObjectId format. The value must be 24 characters long and contain only 0-9, a-z, and A-Z.

  • Field values that match the yyyy-MM-dd'T'HH:mm:ss.SSS'Z' format, such as 2022-01-30T08:00:00.000Z, are automatically converted to the date format. This conversion simplifies conditional queries.

Request example

Insert a record into the users collection.

mpserverless.db.collection('users').insertOne({
    name: 'tom',
    age: 1
})
.then(res => {})
.catch(console.error);

Result example

Successful request:

{
    "affectedDocs": 1,
    "result":  { "insertedId": "630f4ebcf5cf3ad9f434697d", "ok": 1, "n": 1 },
    "success": true
}