Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: MongoDB - assign cursor to database & collection?



Permlink Replies: 7 - Last Post: Apr 18, 2016 5:29 AM Last Post By: SQL Daddy
Bob Johnson

Posts: 28
Registered: 9/11/15
MongoDB - assign cursor to database & collection?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 27, 2016 8:29 AM
How do I assign a MongoDB cursor directly to a specific database and collection? This example code works great, assuming all you want is a list of databases:

procedure TForm1.FormCreate(Sender: TObject);
begin
FDConnection1.Connected := True;
FMongoConn := TMongoConnection(FDConnection1.CliObj);
FDMongoDataSet1.Close;
FDMongoDataSet1.Cursor := FMongoConn.ListDatabases;
FDMongoDataSet1.Open;
end;

But I want a cursor assigned to my collection so I can pull up all it's records into a DB Grid. Assuming database testDB and collection testCollection.

Any help is much appreciated! Thanks!

Edited by: Bob Johnson on Feb 27, 2016 8:34 AM
Dmitry Arefiev

Posts: 1,406
Registered: 12/7/03
Re: MongoDB - assign cursor to database & collection?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2016 7:41 PM   in response to: Bob Johnson in response to: Bob Johnson
You should use TFDMongoQuery.

--
With best regards,
Dmitry
Bob Johnson

Posts: 28
Registered: 9/11/15
Re: MongoDB - assign cursor to database & collection?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2016 8:00 PM   in response to: Dmitry Arefiev in response to: Dmitry Arefiev
Ok, so just to be clear. Do I always use TFDMongoQuery regardless of whether I am reading, inserting, or updating data in the MongoDB database or just in the case of populating dbGrid? I was assuming that I could use TFDMongoQuery to read the data but needed MongoDataSet to update it?

Dmitry Arefiev wrote:
You should use TFDMongoQuery.

--
With best regards,
Dmitry
Dmitry Arefiev

Posts: 1,406
Registered: 12/7/03
Re: MongoDB - assign cursor to database & collection?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 28, 2016 10:25 PM   in response to: Bob Johnson in response to: Bob Johnson
Do I always use TFDMongoQuery regardless of whether I am reading, inserting, or updating data in the MongoDB database or just in the case of populating dbGrid?

There are two options:
* to use TFDMongoQuery for most of CRUD operations;
* to use MongoDB API wrapping classes, including TMongoDocument,
TMongoCollection, TMongoQuery, etc.

TFDMongoQuery is convenient way to work with MongoDB data for Delphi users.
Also because it is TDataSet descendant, it may be used with DB-aware controls,
like TDBGrid. But TFDMongoQuery support of MongoDB data model is limited.
TFDMongoQuery expects, that documents in a single collection have common
structure. Also, when documents have deep structure (with multiple nested
arrays / objects), then TDataSet API becomes not that convenient.

MongoDB API wrapping classes offer maximum flexibility without limitations. But
they are not TDataSet descendants. So, you will need to write code to populate
GUI controls.

I was assuming that I could use TFDMongoQuery to read the data but needed MongoDataSet to update it?

TFDMongoQuery supports as reading as updating. TFDMongoDataSet allows to
attach the dataset to a MongoDB cursor obtained from MongoDB API wrapping
classes. It also supports as reading as updating.

--
With best regards,
Dmitry
Bob Johnson

Posts: 28
Registered: 9/11/15
Re: MongoDB - assign cursor to database & collection?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 29, 2016 6:52 AM   in response to: Dmitry Arefiev in response to: Dmitry Arefiev
Dmitry, I really appreciate your time and help.

So if I continue to use TFDMongoQuery for my CRUD operations, what do I use when it is time to update the database with the db aware controls such as dbGrid, dbMemo, and dbComboBox. It's ugly but currently the only way I could figure out how to save the data reliably was to build an entire Update and running it from an Update button as in:

dmData.FCon['testDb']['testCollection'].Update()
.Match()
.Add('testField1', dbTextField1.Caption)
.&End
.Modify()
.&Set()
.Field('testField2', dbtestField2.Caption)
.Field('testField3', dbtestField3.Caption)
.Field('testField4', dbtestField4.Caption)
.Field('testField5', dbComboBox.Text)
.Field('testField6', dbMemo.Text)
.&End
.&End
.Exec;

Certainly there must be an easier way to accomplish this. But I fell back to this work around because I could not figure it out and nothing else I tried was reliable. How do I Update the current record with changed data?

Thank you so much!

Dmitry Arefiev wrote:
Do I always use TFDMongoQuery regardless of whether I am reading, inserting, or updating data in the MongoDB database or just in the case of populating dbGrid?

There are two options:
* to use TFDMongoQuery for most of CRUD operations;
* to use MongoDB API wrapping classes, including TMongoDocument,
TMongoCollection, TMongoQuery, etc.

TFDMongoQuery is convenient way to work with MongoDB data for Delphi users.
Also because it is TDataSet descendant, it may be used with DB-aware controls,
like TDBGrid. But TFDMongoQuery support of MongoDB data model is limited.
TFDMongoQuery expects, that documents in a single collection have common
structure. Also, when documents have deep structure (with multiple nested
arrays / objects), then TDataSet API becomes not that convenient.

MongoDB API wrapping classes offer maximum flexibility without limitations. But
they are not TDataSet descendants. So, you will need to write code to populate
GUI controls.

I was assuming that I could use TFDMongoQuery to read the data but needed MongoDataSet to update it?

TFDMongoQuery supports as reading as updating. TFDMongoDataSet allows to
attach the dataset to a MongoDB cursor obtained from MongoDB API wrapping
classes. It also supports as reading as updating.

--
With best regards,
Dmitry
Dmitry Arefiev

Posts: 1,406
Registered: 12/7/03
Re: MongoDB - assign cursor to database & collection?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 29, 2016 9:21 PM   in response to: Bob Johnson in response to: Bob Johnson
the only way I could figure out how to save the data reliably was to build an entire Update and running it from an Update button as in:

What means "reliable" in this context ?

Anyway, you does not need to write any code (at least the code, which will
post update to MongoDB). Just do:
FDMongoQuery1.Edit;
<use DB aware controls to edit the data>
FDMongoQuery1.Post;


http://docwiki.embarcadero.com/RADStudio/Seattle/en/Using_Data_Controls
--
With best regards,
Dmitry
Bob Johnson

Posts: 28
Registered: 9/11/15
Re: MongoDB - assign cursor to database & collection?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 1, 2016 9:06 AM   in response to: Dmitry Arefiev in response to: Dmitry Arefiev
Sorry, reliable in the sense of whatever I tried I got inconsistent results. Your post is the solution to all my problems with this. Granted I am basically a beginner and just learning Delphi, but I did not understand the mechanics of this. I knew this was built in to the behind the scenes code but could not find documentation or example of this anywhere. Now it's in my knowledgebase.

FDMongoQuery1.Edit;
<use DB aware controls to edit the data>
FDMongoQuery1.Post;


This works perfectly! You are my hero and new best friend! ;-)

Thank you!

Dmitry Arefiev wrote:
the only way I could figure out how to save the data reliably was to build an entire Update and running it from an Update button as in:

What means "reliable" in this context ?

Anyway, you does not need to write any code (at least the code, which will
post update to MongoDB). Just do:
FDMongoQuery1.Edit;
<use DB aware controls to edit the data>
FDMongoQuery1.Post;


http://docwiki.embarcadero.com/RADStudio/Seattle/en/Using_Data_Controls
--
With best regards,
Dmitry
SQL Daddy

Posts: 6
Registered: 4/8/16
Re: MongoDB - assign cursor to database & collection?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 18, 2016 3:49 AM   in response to: Bob Johnson in response to: Bob Johnson
Hello,

You can Call the cursor.skip() method .This approach may be useful in implementing for more deatils you can visit https://zappysys.com/blog/ssis-loading-data-into-mongodb-upsert-update-delete-insert/

I hope it will be helpful for you.

Edited by: SQL Daddy on Apr 18, 2016 5:29 AM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02