does MongoTalk implement modifier operators? e.g. $set

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

does MongoTalk implement modifier operators? e.g. $set

Sabine Manaa
Hi,

I managed reading and writing my model (e.g. Person objects) into MongoDB with MongoTalk.

But I did use replace:with:

e.g.
"get person from db with OID -
this is a dictionary with ALL OLD fields of the person as stored in the database"
thePersonFromDatabaseDict :=  (self personsCollection queryOne: [ :query | query where: {('_id' -> aPerson mongoObjectId )} asDictionary ])

theNewDict := aPerson mongoDataDict
"dictionary with the NEW fields for this person"

self personsCollection
        replace: thePersonFromDatabaseDict
        with: theNewDict

Which means I replace the whole person object in the database with my current instance data in the image. Also it is much overhead creating the dictionary with all fields from the person in the image.

This is not ok, when I want to set e.g. one field in the person object.

For this reason I want to use the "Modifier operators", e.g. $set
http://docs.mongodb.org/manual/applications/update/
From there:
"Update a Field in a Document. Use $set to update a value of a field. The following operation queries the bios collection for the first document that has an _id field equal to 1 and sets the value of the field middle, in the subdocument name, to Warner:

db.bios.update(
   { _id: 1 },
   {
     $set: { 'name.middle': 'Warner' },
   }
)

I did not find how to use this in MongoTalk. I can not imagine that this functionality is missing but I don't know how to create this from Mongotalk. Unfortunately there is no documentation and as far as I understand there are no test cases for update modifiers.

So, can anyone give me a code snippet how to use update modifiers in Mongotalk?

Sabine
Reply | Threaded
Open this post in threaded view
|

Re: does MongoTalk implement modifier operators? e.g. $set

Yanni Chiu


On 07/03/13 5:51 AM, Sabine Knöfel wrote:
>
> So, can anyone give me a code snippet how to use update modifiers in
> Mongotalk?

Here's a similar question:

http://lists.gforge.inria.fr/pipermail/pharo-project/2012-November/071675.html

The link is to the end of the thread. The relevant text is: "that's how
the driver works: is document-atomic".

So, your Person object is a document, and the MongoDB driver
reads/writes each document all at once (i.e. all the Person attributes
together). You cannot update a Person attribute individually using a
Mongo update, unless you write custom code.



Reply | Threaded
Open this post in threaded view
|

Re: does MongoTalk implement modifier operators? e.g. $set

Sabine Manaa
Please have a look at http://docs.mongodb.org/manual/applications/update/

"The update() method can either replace the existing document with the new document or update specific fields in the existing document."

"Modifier Operators" such as $set, are performed in the context of the database and not at the client.

See also here (german):
http://books.google.de/books?id=6wAJLJRxFt8C&pg=PA156&lpg=PA156&dq=%22Modifier-Operatoren+und+die+damit+%22+MongoDB&source=bl&ots=1VdUhW6VEG&sig=_wG4cMTzZnngpHFEYPt3tLO8-40&hl=de&sa=X&ei=LJg5UYUY0cO0Bv3agbgJ&ved=0CEgQ6AEwAg#v=onepage&q=%22Modifier-Operatoren%20und%20die%20damit%20%22%20MongoDB&f=false

In this book, the autor writes, that you will go to database hell, if you always update the whole document instead of single fields. :-)

What are the experiences of bigger projects using MongoTalk - do you always write the whole collection? What about the performance with this solution? Is this feasible? It sounds strange to me.
Reply | Threaded
Open this post in threaded view
|

Re: does MongoTalk implement modifier operators? e.g. $set

EstebanLM
I have an app who has large documents that are saved each time someone modifies it... since is not a frequent operation, the app performs really well.
why would be strange? a document is not more than a bunch of dictionaries... of course in certain cases you can optimize, but most of the time you don'y need it.

Esteban

On Mar 8, 2013, at 8:59 AM, Sabine Knöfel <[hidden email]> wrote:

> Please have a look at http://docs.mongodb.org/manual/applications/update/
>
> "The update() method can either replace the existing document with the new
> document* or update specific fields in the existing document.*"
>
> "Modifier Operators" such as $set, are performed in the context of the
> database and not at the client.
>
> See also here (german):
> http://books.google.de/books?id=6wAJLJRxFt8C&pg=PA156&lpg=PA156&dq=%22Modifier-Operatoren+und+die+damit+%22+MongoDB&source=bl&ots=1VdUhW6VEG&sig=_wG4cMTzZnngpHFEYPt3tLO8-40&hl=de&sa=X&ei=LJg5UYUY0cO0Bv3agbgJ&ved=0CEgQ6AEwAg#v=onepage&q=%22Modifier-Operatoren%20und%20die%20damit%20%22%20MongoDB&f=false
>
> In this book, the autor writes, that you will go to database hell, if you
> always update the whole document instead of single fields. :-)
>
> What are the experiences of bigger projects using MongoTalk - do you always
> write the *whole *collection? What about the performance with this solution?
> Is this feasible? It sounds strange to me.
>
>
>
> --
> View this message in context: http://forum.world.st/does-MongoTalk-implement-modifier-operators-e-g-set-tp4675511p4675675.html
> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.
>


Reply | Threaded
Open this post in threaded view
|

Re: does MongoTalk implement modifier operators? e.g. $set

Yanni Chiu
In reply to this post by Sabine Manaa


On 08/03/13 2:59 AM, Sabine Knöfel wrote:
>
> What are the experiences of bigger projects using MongoTalk - do you always
> write the *whole *collection?

That was the confusion in the other thread. The whole *Smalltalk*
Collection instance holding DateAndTime objects is written out, because
the DateAndTime instances are not made separate documents.

MongoTalk does not re-write the whole MongeDB collection. However, it
currently does write out the full document into each mongo collection.
If your objects hold image data (or other large data items), then it can
be a problem to re-write the entire attribute set.

It may be possible to add something like the Magritte memento framework,
to be able to use mongo update operations.



Reply | Threaded
Open this post in threaded view
|

Re: does MongoTalk implement modifier operators? e.g. $set

Yanni Chiu
In reply to this post by EstebanLM


On 08/03/13 4:41 AM, Esteban Lorenzano wrote:
> a document is not more than a bunch of dictionaries... of course in certain cases you can optimize, but most of the time you don'y need it.

My very small app was almost done, before I came across
MongoTalk/Voyage. I used the MongoDB connection directly, and wrote it
to use the mongodb UPDATE operations (because that's how all the mongodb
examples did it).

If I'd started with MongoTalk/Voyage, then I would probably just accept
the re-writing of the whole document.



Reply | Threaded
Open this post in threaded view
|

Re: does MongoTalk implement modifier operators? e.g. $set

Sabine Manaa
I do currently not save big documents or pictures within the person but I will save things like the name of the view which was clicked each time the user changes to another view. I think saving the whole person data (address, bank data etc) would create much overhead. I also save travel data with receipts, vehicle trips etc. Changing the name of a trip should not lead to saving the whole trip with its vehicle and receipt data....So for me, using update operations would make much sense. And perhaps, later, documents could be added, too.

Sorry, I added confusion, I meant:

"What are the experiences of bigger projects using MongoTalk - do you always write the whole DOCUMENT? (and not collection)"

Yanni, you wrote
"I used the MongoDB connection directly, and wrote it to use the mongodb UPDATE operations (because that's how all the mongodb examples did it). "

Yanni, I understand, that you have some code to perform UPDATE operations from Smalltalk? Could you please send me some examples?

Greetings
Sabine
Reply | Threaded
Open this post in threaded view
|

Re: does MongoTalk implement modifier operators? e.g. $set

Yanni Chiu
On 08/03/13 11:26 AM, Sabine Knöfel wrote:
>
> Yanni, you wrote
> "I used the MongoDB connection directly, and wrote it to use the mongodb
> UPDATE operations (because that's how all the mongodb examples did it). "
>
> Yanni, I understand, that you have some code to perform UPDATE operations
> from Smalltalk? Could you please send me some examples?

Oops, when I looked back my code, I found that I had started the project
using node.js. When I was doing the mongodb UPDATE, that was still being
coded in node.js. (I grew frustrated with node.js, and moved back to the
comfort of Seaside).

Looking at the Mongo code in Pharo, I think you can achieve what you
want by calling:

MongoCollection>>update: origDictionary with: newDictionary

Set up the 'origDictionary' to have key/value pairs for only the
identifying key attributes - e.g. {'person-id' -> 'Johan'}

Set up the 'newDictionary' to have key/value pairs for only the
attributes whose values need to be updated.

Warning - I've never tried this, but it looks like it should work.


Reply | Threaded
Open this post in threaded view
|

Re: does MongoTalk implement modifier operators? e.g. $set

jan.struz
In reply to this post by Sabine Manaa
Sabine Knöfel wrote
db.bios.update(
   { _id: 1 },
   {
     $set: { 'name.middle': 'Warner' },
   }
)
Hi, the MongoTalk equivalent of this is:
       
        what := { 'name.middle' -> 'Warner' } asDictionary.
        self bios
                update: { '_id' -> 1 } asDictionary
                with: { '$set' -> what } asDictionary.

you can set multiple attributes, of course:

        what := { 'attr1' -> 'value1' . 'attr2' -> 'value2' } asDictionary.

JS.
Save The World!
Reply | Threaded
Open this post in threaded view
|

Re: does MongoTalk implement modifier operators? e.g. $set

Yanni Chiu
In reply to this post by Yanni Chiu
On 08/03/13 1:30 PM, Yanni Chiu wrote:
 >
 > Set up the 'newDictionary' to have key/value pairs for only the
 > attributes whose values need to be updated.
 >
 > Warning - I've never tried this, but it looks like it should work.

It was close, but see what jan.struz says in his reply.
The 'newDictionary' should be {'$set' -> newDictionary}.
I hope I didn't steer you the wrong way.


Reply | Threaded
Open this post in threaded view
|

Re: does MongoTalk implement modifier operators? e.g. $set

Sabine Manaa
Hi Yanni, Hi Jan,

thanks a lot. That works perfect!
You did not bring me on the wrong way, I was out for skiinig the weekend and did not work.

Sabine