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
> --
> 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.
>
>
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
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.