Add new attribute to existing document - python-3.x

How do I add an attribute to all documents in collection using mongoengine?
I've tried several ways:
for d in MyDocuments.objects:
d.attr = 'test'
d.save()
This doesn't give any errors but it doesn't add new attribute to document.
I've also tried
MyDocuments.objects.all().update(set__attr='test')
This gives InvalidQueryError: Cannot resolve field "attr"
The same error happens when I try
for d in MyDocuments.objects:
d.update(set__attr='test')
Specifically I'm trying to add _cls attribute because I enabled inheritance and I already have some objects in database that don't have that attribute.

Finally this worked for me:
MyDocuments._get_collection().update_many({}, {'$set': {'_cls': 'MyDocuments.TTR'}})

Related

Remove property from mongoose document instance

I need to remove a property from a mongoose document instance. I've found a lot of questions that show how to remove it from the database, but that's not what I'm looking for.
I need to pull the document down including a field to check security access, I then want to strip that field so that it doesn't get disclosed if downstream code decides to call toObject() and send the object back to the client.
Any thoughts?
I needed to remove password property from the document instance but I didn't find anything in the API documentation. Here is what I did:
doc.set('password', null); // doc.password is null
Then I found you can also do this:
delete doc._doc.password // doc.password is undefined
Since version 2.4 you can do:
doc.field = undefined;
await doc.save();
This will essentially $unset the field
Using the set function with a value of null will simply assign the value, not remove it. Best to first convert the document using toObject() (so that it is becomes a plain Object), make the changes and revive it back to a model document:
let tempDoc = doc.toObject();
delete tempDoc.password;
doc = new this(tempDoc);

kohana ORM factory does not show newly added column in database

I get this error,when i added a new column 'custom_url'.
I already had an instance of the ORM,when i fetch or try to save data for new column it gives me error saying that custom_url property does not exist.
Kohana_Exception [ 0 ]: The custom_url property does not exist in the Model_Event class
Are you using PDO driver? Because then you manually need to add all columns to the model's _table_columns.

Remove collection from model RacerJS?

This is my client Side code:
model.remove('agent',{'text':'online'});
I cant able to remove collection from model,Its shows following error in console
Error: remove must be performed under a collection and document id. Invalid path: agent
From the documentation, it looks like 'path' should be the collectionname.id. In this case, maybe I suppose it should be 'agent.id'.
This blog refers that the path should be in the format collection.documentId.document.
http://blog.derbyjs.com/2012/04/13/derby-v0-dot-3-0/

"EntityState must be set to null, Created (for Create message) or Changed (for Update message)" when attempting to update an entity in CRM 2011

I am using the following code to update an entity.
Service.Update(_policy);
where policy is a class generated using CrmSvcUtil.exe
public partial class new_policy : Microsoft.Xrm.Sdk.Entity, System.ComponentModel.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged
I retrieve the policies using LINQ, then update one attribute (an EntityReference) and then attempt the update
When this code runs I get the following error message:
EntityState must be set to null, Created (for Create message) or
Changed (for Update message)
There are other entities generated in the same way that I can update.
I tried
_policy.EntityState = EntityState.Changed
but then I get a message saying
The entity is read-only and the 'EntityState' property cannot be
modified. Use the context to update the entity instead.
Does anyone know what is causing this?
You have to tell your crmContext (use appropriate name) what to do with the changes.
You should add crmContext.UpdateObject(contact); before crmContext.SaveChanges();
See also How to update a CRM 2011 Entity using LINQ in a Plugin?
To avoid the problem you can simply use update-helper-objects instead of using the retrieved record:
var policyUpdater = new Policy { Id = _policy.Id, FieldToUpdate = "newValue" };
service.Update(policyUpdater);
Note: Properties of the update-helper-object that aren't set are simply ignored. The update won't set the corresponding record fields to null
I had the same problem. I switched from using
context.Update(object)
to
context.UpdateObject(object)
and it worked.
This worked for me:
recordToUpdate.EntityState = EntityState.Changed;
(recordToUpdate is an Entity to be updated)
Turns out it was an issue with my linq query that was retrieving the entity in the first place. When I replaced this with a query expression it worked okay.
Time to brush up on my linq!

LINQ-to-NHibernate Filter IQueryable by Composite Field - "Could not resolve property" error

Quick background - I have a form that offers a handful of optional options to users and a search method on my service that accepts all those fields and attaches the necessary Where() conditions on the master IQueryable list.
One of those filters is a list of strings that must be compared to a combination of three different fields in the IQueryable. here's the code throwing the "could not resolve property" error:
var searchResults = _transactionHeaders.Retrieve();
if (subgroups.Any())
searchResults = searchResults.Where(s => subgroups.Contains(s.CustomerType + s.RusNumber + s.GroupNumber));
return searchResults.ToList()
I've read a few posts that suggest an alias needs to be created for any properties not directly mapped in the NHibernate mapping. I'm not quite sure that is the solution to my problem.
Suggestions? Thanks for any help you can offer.
Linq2Nhibernate can't understand a .Contains method call to. You'll have to change your query so it's compatible with linq2nhibernate.

Resources