FireDAC.Phys.MongoDBWrapper.TMongoCollection.Insert

From RAD Studio API Documentation
Jump to: navigation, search

Delphi

procedure Insert(ADocument: TMongoDocument; AFlags: TInsertFlags = []); overload;
procedure Insert(AInsert: TMongoInsert; AFlags: TInsertFlags = []); overload;
function Insert(AFlags: TInsertFlags = []): TMongoInsert; overload;

C++

void __fastcall Insert(TMongoDocument* ADocument, TInsertFlags AFlags = TInsertFlags() )/* overload */;
void __fastcall Insert(TMongoInsert* AInsert, TInsertFlags AFlags = TInsertFlags() )/* overload */;
TMongoInsert* __fastcall Insert(TInsertFlags AFlags = TInsertFlags() )/* overload */;

Properties

Type Visibility Source Unit Parent
procedure
function
public
FireDAC.Phys.MongoDBWrapper.pas
FireDAC.Phys.MongoDBWrapper.hpp
FireDAC.Phys.MongoDBWrapper TMongoCollection

Description

Inserts the specified document into the collection or executes the specified insert builder on the collection.

To work with an insert builder, Insert supports two different coding styles.

You can optionally specify a set of insert flags.

If the insert command succeeds, you can obtain the number of inserted documents from the DocsInserted property.
If the insert command fails, it raises an exception.

Insert can only insert a single document. To insert multiple documents in a single operation, perform your Insert calls between calls to BeginBulk and EndBulk.

Example

The following example inserts a new document.

MongDB shell:

db.restaurants.insert({
  "name": "La Bella Italia",
  "cuisine": "Italian",
  "grades": [
    {
      "date": ISODate("2015-11-02T14:38:00Z"),
      "grade": "A",
      "score": 8
    }
  ]
})

Delphi:

MyDocument := TMongoDocument.Create(MyConnection.Env)
  .Add('name', 'La Bella Italia')
  .Add('cuisine', 'Italian')
  .BeginArray('grades')
    .BeginObject('')
      .Add('date', ISO8601ToDate('2015-11-02T14:38:00Z'))
      .Add('grade', 'A')
      .Add('score', 8)
    .EndObject
  .EndArray;
MyCollection.Insert(MyDocument);
MyDocument.Free;

C++:

TMongoDocument* MyDocument = new TMongoDocument(MyConnection->Env);
MyDocument
  ->Add("name", String("La Bella Italia"))
  ->Add("cuisine", String("Italian"))
  ->BeginArray("grades")
    ->BeginObject("")
      ->Add("date", ISO8601ToDate("2015-11-02T14:38:00Z"))
      ->Add("grade", String("A"))
      ->Add("score", 8)
    ->EndObject()
  ->EndArray();
MyCollection->Insert(MyDocument);
delete MyDocument;

See Also