Model Methods: InsertOne

Inserting a document into the database collection in MerlinDB.

🪶 MerlinDB's insertOne method is used to add a document to a collection.

The insert, insertOne, insertMany methods are similar, the only thing that differentiates them is that the 'insertOne' method allows only one document to be inserted into the database, the 'insertMany ' allows multiple documents to be inserted, and 'insert' allows both.

1
  • import
  • MerlinDB,
  • {
  • Schema
  • }
  • from
  • "@chrisaxxwell/merlin-db";
    2
  • //Or if you are using 'merlindb.max.js' just call 'new MerlinDB()';
  • 3
    4
  • const
  • merlin =
  • new
  • MerlinDB(
  • )
  • ;
    5merlin
  • .connect(
  • "USER-DATABASE"
  • )
  • ;
    6
    7
  • const
  • UserSchema =
  • Schema(
  • {
  •   
    8  
  • name:
  • String
  • ,
    9  
  • age:
  • Number
  • 10
  • }
  • )
  • ;
    11
    12
  • var
  • userModel = merlin
  • .model(
  • "User", UserSchema
  • )
  • ;
    13
    14
  • //userModel.insertOne(<data(Object)>, <options>);
  • 15userModel
  • .insertOne(
  • {
  • 16  
  • name:
  • "Isabelle",
    17  
  • age:
  • 25
  • 18
    19
  • }
  • ,
  • {
  • ordered:
  • true
  • }
  • )
  • 20
  • //Currently it provides the option to order, default is true;
  • 21
  • //It orders the data in the database and returns the $order in queries
  • 22
  • //By default all data is (ordered: true) ordered by timestamp when entered.
  • 23
  • .then(
  • e =>
  • {
  • 24   console
  • .log(
  • e
  • )
  • ;
    25  
  • //returns
  • 26  
  • {
  • 27      
  • acknowledged:
  • true
  • ,
    28      
  • insertedI:
  • "4As32...",
    29      
  • data:
  • {
  • name:
  • "Isabelle",
  • age:
  • 25
  • ,
  • id_:
  • "4As32..."
  • }
  • 30  
  • }
  • 31
  • }
  • )
  • .catch(
  • e =>
  • {
  • 32   console
  • .log(
  • e
  • )
  • ;
    33  
  • //Error message...
  • 34
  • }
  • )