Update one property's value to the value of another property in the same document - mongodb

How can we replicate the following sql query in MongoDb?
update Person set Alias = Name;
where the Person table has the columns Alias, Name
I want the query to affect multiple rows. It doesn't matter if the update query cannot support upserts, I only need to update & not insert.

Unfortunately, that functionality is not available in MongoDB. You will need to loop through the documents, updating them one-at-a-time, and doing a read-update pair.
If you want this to be concurrency-safe, you'll need to implement some sort of locking; either optimistic or pessimistic.

myDb.myCollection.find({}).forEach(
function (person) {
person.Alias = person.Name
myDb.myCollection.save(person)
}
)

Related

Dynamic Sorting according to populated value?

There is a table named house which has many-to-many association with location table. Have to sort the list by the value of the location.street. Is there a way to sort the result of the query according to the value of the populated field?
I tried:
House.populateAll().sort("location.street ASC").exec(console.log);
Neither waterline (0.10.22) or sailsjs (v0.11) currently support this.
You would need to process your return variable to reorder the data.
There is a ticket for this at https://github.com/balderdashy/waterline/issues/334

When using an extbase query, can I order the results by a field in a linked model?

I am querying a Model*, which has a field that refers to another Model. Is it possible to order the results using a field in the linked Model?
So for example:
Car contains a field which refers to its OWner. I want to show all Cars sorted by their owner.
(I don't want to use the statement() method since in that case I would have to write the whole query myself)
(*) using http://typo3.org/api/typo3cms/class_t_y_p_o3_1_1_c_m_s_1_1_extbase_1_1_persistence_1_1_generic_1_1_query.html
Yes, it is possible (don't care for the $constraint for the moment):
$query->matching($constraint)->setOrderings(
array('owner.sorting' => Tx_Extbase_Persistence_QueryInterface::ORDER_ASCENDING)
)->execute();
Assuming the field in you Car model is named owner and you want to sort by sorting field of the Owner model/table.
When working with 6.2
->setOrderings(Array('model.yourfield' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING))
#Michael and #rob-ot are right. There is one pitfall that took me many hours and that I'd like to mention here:
If your sort field in your related table contains underscores, you have to provide its name to setSortOrderings in lowerCamelCase:
// with database column name my_car_sorting you must define:
$query->matching($constraint)->setOrderings(
array('owner.myCarSorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING)
)->execute();

Retrieve data from relative table while sorting it by date field

I have a schedule entity which is in 1:n relation with a broadcast entity.
When I do somewhat like mBroadcasts = schedules.get(0).getBroadcastList(); it sorts data by id. I need to sort data by date field of the broadcast entity.
Thanks.
You can use a QueryBulder for create a select. For example, if you want to order by property date:
daoSession.getBroadcastDao().queryBuilder().orderAsc(Properties.Date);
If you need to create a complex condition WHERE...
QueryBuilder<Broadcast> qb = daoSession.getBroadcastDao().queryBuilder();
and then, add and, or, equal, lt, gt, ... or whatever you need.

How do I ignore the created column on a Zend_DB table save?

how would I ignore having Zend_DB save() from trying to fill out a created column? I do not need that column for a certain model.
Don't send the data. save() is part of the Zend_Db_Table_Row api and is designed to be somewhat intelligent in the way it saves data to a row. It will perform an insert or an update of a row depending on what is required.
save() will also only update the columns that it has data for. If you don't send new data for your created column save() won't overwrite the data.
When ever it is possible I let the database I'm using create and update the columns for created and updated. That way I have the information available to query if I need it but I don't have to do something with PHP that My database can do better.
Check out http://framework.zend.com/manual/1.12/en/zend.db.table.html Section "Advanced usage".
For more specific and optimized requests, you may wish to limit the
number of columns returned in a row or rowset. This can be achieved by
passing a FROM clause to the select object. The first argument in the
FROM clause is identical to that of a Zend_Db_Select object with the
addition of being able to pass an instance of Zend_Db_Table_Abstract
and have it automatically determine the table name.
Important
The rowset contains rows that are still 'valid' - they simply contain
a subset of the columns of a table. If a save() method is called on a
partial row then only the fields available will be modified.
So, if you called an update() I think it would be as simple as unsetting the value for the column you don't want to touch. Of course database constraints will need to be honored - i.e. column should allow nulls.

Linq To Entities - How to create a query where the table name is a parameter

Dynamic queries are not dynamic enough. I have seen solutions like this but still I have to indicate which table to use as the basis:
var query = db.Customers.Where("...").OrderBy("...").Select("...");
I want to create a simple query tool where the user will select from available tables using a drop-down list. As the result, I want to show first few records. Therefore, I need to change the table too! That is, I need something like this:
string selectedTable = "Customers";
var [tableName] = SomeTypecastingOperations(selectedTable);
var query = db.[tableName].Where("...").OrderBy("...").Select("...");
Is EF dynamic enough to handle this?
Linq-to-entities doesn't support that. You can achieve that with Entity SQL or some ugly code which will have conditional logic for every set you want to query (like a big switch for table names).