$push / $addToSet if array, $set if not

717 views
Skip to first unread message

Jeffrey Jones

unread,
Dec 20, 2010, 9:57:16 PM12/20/10
to mongod...@googlegroups.com
Hello all,

New to mongoDB and currently researching possibilities. I have been
looking through the docs, forum history, google etc. and cannot find
anything obvious but I cannot believe that I am the first person to ask
this question.

I have a document where some of the fields are arrays of values. I do
not know ahead of time which fields might be arrays because my app is
acting as a dumb HTTP API server interface to MongoDB which is used by
other apps. In this case we cannot expose MongoDB directly.

Is there a single command I can do whereby I can update a record and
mongodb automatically $sets fields that hold a single value and $pushes
/ $addsToSet values to fields which are already arrays without me having
to specify?

Some sort of combination command I can use which will $set and
$push/$addToSet when appropriate? Something along the lines of:

db.people.update( { name:"Joe" }, { $set_and_push: { age : 18,
favourite_colours : Red } } );
db.people.update( { name:"Joe" }, { $set_and_addToSet: { age : 18,
favourite_colours : Red } } );


Which in this case would update Joe's age and add Red to the array of
his favourite colours (uniquely depending on the command).

If this isn't possible then as far as I can see it means that at runtime
I have to retrieve the record. Convert the BSON into native types (Ruby
dev in this case), check the class of the field value (String, Array
etc.) then run a $set update for all the single value types and then
individual $push updates for each array type field.

This sounds overly complicated and would be a heck of a lot slower I
imagine, I hope I am missing something obvious which will mean I don't
need to do this.

Any advice would be much appreciated.

Jeff

Eliot Horowitz

unread,
Dec 21, 2010, 2:32:20 AM12/21/10
to mongod...@googlegroups.com
I don't think what you're looking for exists.
Would be a little strange as when you're using it you can't tell what
the output would be...

> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mongodb-user?hl=en.
>
>

Sam Millman

unread,
Dec 21, 2010, 3:11:59 AM12/21/10
to mongod...@googlegroups.com
Cant you get the type of a field by some other means? Say you changed the programming within apps very slightly to transmit a pre-defined field type so you knew what type of field you are writing to? Should be quite easy from my point of view. However if this is a type of abstraction layer you will find problems with more complex commands. I find drivers are designed to their api extended instead of replaced.

On the other hand there are certain commands that will throw exceptions when you try to use certain commands on them. Since pinging Mongo is very fast you could use these commands, catch their exceptions and judge by those means what field type you have.
--
Bow Chicka Bow Wow

Jeffrey Jones

unread,
Dec 21, 2010, 6:58:04 PM12/21/10
to mongod...@googlegroups.com
Hello all,

Apologies if this starts a new thread. I was unable to post via the web
interface and had the group setup as web-only.

There will be no complicated commands via this interface, just very
simple crud.

I also have to write the client libraries which will use this interface
so I guess I could modify them to send field => type pairs based on what
they think the types should be.

In terms of data access it is quite a chaotic system. Lots of different
clients updating the same data but in very simple ways. In this scenario
the only single source of authority on what the current field types are
is mongoDB itself which is why I was loath to have to farm that out to
something else.

Since the stored data will literally be only values and arrays of values
another option would be to have a predefined naming scheme for fields
(a_* is always an array), it seems a bit restrictive but probably a lot
less painful than hacking up any other methods.

Regards

Jeff

Sam Millman

unread,
Dec 22, 2010, 3:40:06 AM12/22/10
to mongod...@googlegroups.com
I believe mongo only knows data types etc when it tries to operate on them. When it receives an error when for example:

 - Using addToSet on a string value

It will know you are trying to place the wrong value in the wrong field.

If I'm right (could be wrong) the mongo driver is actually dumb to data types giving it extra speed.



Jeff

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.

Reply all
Reply to author
Forward
0 new messages