What is the difference between findOneAndUpdate and update?
Both accept criteria to query and doc to update.
Well there is the respective documentation to view for both .update() and .findAndModify() which is the root method of .findOneAndUpdate() here.
But in the main differences there are:
update(): Is meant to perform an atomic update operation against "one or more" documents matched by it's query condition in a collection. It returns the number of modified documents in it's response.
findOneAndUpdate(): Has the purpose of both processing an update statment on a "singular" document, as well as retrieving the content of that "singular" document. The state returned depends on the value of the "new" option as passed to the operation. Where true the "modified" document is returned. Where false the "original" document is returned before any modification. The latter form is the default option.
In short. One is meant to modify in "bulk" and not worry with the document content in result. And the other is meant to modify a singular document and return the document content in result.
That's the difference.
The .findOneAndUpdate method issues a mongodb .findAndModify update command and returns the found document (if any) to the callback or return the modified document rather than the original if the new option is true and the .update execute the query as an update() operation.
Note there is an option returnNewDocument in the findOneAndXXX methods and it's default value is true. If you are using the node.js driver, the options is called returnOriginal.
Related
I have been trying to add data to sections in my firestorm database, I have a collection -> document -> data fields. Whenever I use setData({'key': 'value'}) is always overwrites the data already in the document. Is there anyway around this?
That is because what setData(object) is used for
To create or overwrite a single document
While update(object)
To update some fields of a document without overwriting the entire document
So what you need is to use update()
Using merge: true in your setData() statement prevents overwrite.
This seems similar to the update() method but you can use it even when document does not exist (ie is being created).
Before i checked in the afterFind callback if the result of the find is empty.
Since the callback was removed in latest versions, where would be the place to check that from a behavior?
Im not realy sure if that is what i need. My use case is, i want to find out if a query has no result. Then i would create a default entry so it has 1 result.
https://book.cakephp.org/3.0/en/appendices/orm-migration.html#no-afterfind-event-or-virtual-fields
Once defined you can access your new property using $user->full_name. Using the Modifying Results with Map/Reduce features of the ORM allow you to build aggregated data from your results, which is another use case that the afterFind callback was often used for.
Call count() on the ResultSet object
https://api.cakephp.org/3.4/class-Cake.ORM.ResultSet.html#_count
No idea why you're not using that and want to do the count manually, but go on.
Using resultFormatter()
https://book.cakephp.org/3.0/en/orm/query-builder.html#adding-calculated-fields
You behavior will have to add the formatter in your beforeFind() to the query. You can add a custom find that adds it as well. Example code (taken from the docs):
$query->formatResults(function (\Cake\Collection\CollectionInterface $results) {
return $results->map(function ($row) {
$row['age'] = $row['birth_date']->diff(new \DateTime)->y;
return $row;
});
});
Count the results there.
Using map/reduce
https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#map-reduce
More often than not, find operations require post-processing the data that is found in the database. While entities’ getter methods can take care of most of the virtual property generation or special data formatting, sometimes you need to change the data structure in a more fundamental way.
You behavior will have to add the mapper/reducer in your beforeFind() to the query. You can add a custom find that adds it as well. Example code (taken from the docs):
$articlesByStatus = $articles->find()
->where(['author_id' => 1])
->mapReduce($mapper, $reducer);
Check the above link for a detailed explanation including examples of how to create a mapper and reducer.
Our Solr is configured to return ALL results if no valid search parameters are passed in. For example:
http://localhost:8983/solr/collection1/select?rows=1&title=bar is a valid search (title is a valid field) and it returns the proper number of results (1 out of many results). But... http://localhost:8983/solr/collection1/select?rows=1&foo=bar returns one out of the entire collection (foo is not a valid field).
I read that there is a way to configure Solr to return NO results by default (instead of all). It said "adjust the requestHandler config to return all results by default" (which I assume means there is a way to return none by default) but I cannot find anything online about how to actually do this.
The reason we want this is because we're implementing a blacklist of fields that we don't want the user to search on, but by doing this, it allows all other fields through and we'd like those to return no results (or even better - an error saying the field is invalid).
Solr is being called through our API that we wrote, so even if we could add on a parameter to each call to make it return no results by default (noResultsIfNoValidSearch=true or something), that would work.
So, any ideas on how to configure Solr to return NO results by default? Thanks!
Add echoParams=all into your query to see everything that the request has, coming from all configuration sources.
Most likely you define q=*:* somewhere in your configuration, that's what causing returning everything. Remove that and you should get nothing.
If you are using eDixMas, you can look into uf parameter which allows to restrict the fields users are allowed to query.
To allow all fields except title, use uf=*-title
The easiest thing that comes to mind is to set the rows parameter to 0 in your API or config, depending on your requirements.
I am using Talend to extract data from Salesforce, but no matter what i try, when i try to pull data from a custom object, all i ever get is a null value.
I am using Talend 6.1, and the version of salesforce is 33.
I have created a simple tsalesForceInput to tlogrow routine, but it isn't working.
In the tSalesforceInput I have the following options selected:
Module: Custom Object
Custom Object Name: LLC_BI__Loan__c
Manual input of SOQL query: True
select
LLC_BI__Account__c
FROM LLC_BI__Loan__c
Please can anyone help me? I have checked, and there is data populated in this field for each record returned, but it will only ever display null.
Turns out the issue was regarding the schema not matching exactly on the tLogrow. Once this was amended to have column as LLC_BI__Account__c it worked (I think i had missed the double underscore).
I'd like to know whether default values are applied at the time of instance creation, instance update, or only when attempting to read the value. I'd like to understand how this applies to queries.
For example, suppose I initially create a model w/o default values
class Foo (db.Model):
bar = db.BooleanProperty()
Then I create and put a few instances of Foo.
Then later I update the model
class Foo (db.Model):
bar = db.BooleanProperty(default=False)
And then I have a query
foos = Foo.all().filter('bar =', False)
Will the result include the instances of Foo that were created prior to adding the default value to the model definition?
And if I instead query as
foos = Foo.all().filter('bar !=', True)
Does this include the instances of Foo that were created prior to adding the default value to the model definition? Is it (in the case of a Boolean Property) and wrt default values any different to the previous query?
I looked for docs on this, but found none, sorry. Hopefully I'm just missing them. If possible, please provide links to GAE docs.
Update:
I found this in the docs
Entities lacking a property named in the query are ignored
Entities of the same kind need not have the same properties. To be eligible as a query result, an entity must possess a value (possibly null) for every property named in the query's filters and sort orders. If not, the entity is omitted from the indexes used to execute the query and consequently will not be included in the query's results.
Note: It is not possible to query for entities that are specifically lacking a given property. One alternative is to define a fixed (modeled) property with a default value of None, then filter for entities with None as the value of that property.
https://cloud.google.com/appengine/docs/python/datastore/queries
Assuming that default values are applied at time of entity creation or update, I believe that means that neither of the above queries would return entities that were last put prior to the default value being added to the model. Is that right?
thanks
It really depends. If you actually put a "bar" in your foo prior to having the default value or not.
1- In the case you never set bar and just "put" foos, then yes, anything you put prior to adding the default value will have nothing as their "bar".
2- In the case you actually programaticaly setup bars for your foos as you create them, then your query will return everything.
The way you phrased your question makes me believe you had it setup with # 1, so no, anything you put in your datastore before the default value wouldn't be returned. You will need to go through your whole datastore and update your foos.
While not in the official documentation, this has been validated in comments by Alex Martelli (a Googler)
The default values are assigned only on when left untouched on entity creation. Updates will keep the original value.
This default value doesn't affect queries in any way, indexes are written when you save the entity, so you would need to write the entity again with the proper value.