I have created the database name as bitnomy and saving the data to the collection name transactionModel, but when I hit the request it save to some collection name transactionmodels (which is nowhere link).
Can any one help me in this?
Given that you are using mongoose you can force the collection name during the Schema creation:
var UserInfo = new Schema({
username : String,
birthDate : Date
}, { collection: 'userinfo' });
This because by default mongoose pluralise the collection names, in this case would be users
Related
I'm trying to use sObject to dynamically change Name field objects across while organization.
I've tried using SomeId.getSObjectType().newSObject(SomeId) to create the sObject, but when I try to change the Name field I have error
Variable does not exist: Name
Map<Id, string> idsToUpdate = new Map<Id, string>();
// Put the Id's and associated name values in the map
List<SObject> sObjectsToUpdate = new List<SObject>();
foreach(Id idToUpdate : idsToUpdate.keySet) {
SObject o1 = idToUpdate.getSObjectType().newSObject(idToUpdate);
o1.Name = idsToUpdate.get(idToUpdate);
sObjectsToUpdate.add(o1);
}
update sObjectsToUpdate;
As I can see other posts, this is the way of creation dynamic update of objects.
Any idea why this happens?
Not all objects have a name field, you should check for the existence of the name field before trying to set the field also you must use the put method
Map <String, Schema.SObjectField> fieldMap = o1.getSobjectType().getDescribe().fields.getMap();
if(fieldMap.containsKey('Name')){
o1.put('Name', 'Test');
}
How to validate if a Model parameter (Example name) already exist in another model in the Collection?
You can use Underscore methods to find other models in the collection. So if you are looking for a specific name in the collection you could do:
var searchName = 'name from the model you are looking for';
var items = this.model.where({ name: name }); // model is a collection.
if (items.length) {
// Validation logic.
}
I've tried simple example from backbone tutorial and can't get this working
var Person = Backbone.Model.extend({});
var People = Backbone.Collection.extend({
model: Person,
url: "http://localhost:3002/people"
});
var people = new People();
var person = new Person({
id: 3
});
person.fetch({
success: function () {
person.set({age: 23});
person.save();
}
});
I just want to update existing record with id equals to 3 but got an error A "url" property or function must be specified. I'm sure that I didn't make mistake when typing this example but it works in tutorial and doesn't work for me. Is it because of some version changes?
As the error and comments have indicated, you need to specify the url property for the model or add the person model the people collection.
If you would like to fetch your model using the same url as the people collection. You need to add person to the people collection by doing:
var people = new People(person);
// or
people.add(person);
// The fetch url for a person would look like
// GET http://localhost:3002/people/3 Assuming the id of the person is 3.
If you need to use a different url than your collection has specifed the person model. You can specify the url or urlRoot attribute in your Person model.
var Person = Backbone.Model.extend({
urlRoot:'http://localhost:3002/person'
});
// The fetch url for a person would look like
// GET http://localhost:3002/person/3 The number will match id of model.
I'm currently developing a mobile application who uses a Google App Engine-hosted web service.
But i'm facing an issue. I just want to add a field in one my database's table.
App Engine doesn't use classic SQL syntax, but GQL. So i cannot use the ALTER TABLE statement. How can i do this with GQL ? I looked for a solution on the web, but there's not a lot of help.
public MyEntity() {
}
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Key idStation;
private String name;
private double longitude;
private double latitude;
private java.util.Date dateRefresh = new Date(); //the field i want to add in DB
So, now when i create a "MyEntity" object, it should add the "dateRefresh" field into the database... I create my object like this:
MyEntity station = new MyEntity();
station.setName("test");
station.setLatitude(0);
station.setLongitude(0);
station.setDateRefresh(new Date("01/01/1980"));
DaoFactory.getStationDao().addStation(station);
addStation method:
#Override
public MyEntity addStation(MyEntity station) {
EntityManager em = PersistenceManager.getEntityManagerFactory().createEntityManager();
try {
em.getTransaction().begin();
em.persist(station);
em.getTransaction().commit();
} finally {
if(em.getTransaction().isActive()) em.getTransaction().rollback();
em.close();
}
return station;
}
The field "dateRefresh" is never created into my DB...
Someone to help me please ?
Thanks in advance
Just add another field to your data structure, maybe providing a default clause, and that's all. For example, if you have a UserAccount:
class UserAccount(db.Model):
user = db.UserProperty()
user_id = db.StringProperty()
you may easily add:
class UserAccount(db.Model):
user = db.UserProperty()
user_id = db.StringProperty()
extra_info = db.IntegerProperty(default=0)
timezone = db.StringProperty(default="UTC")
and let it go.
While the datastore kinda mimics tables, data is stored on a per entity basis. There is no schema or table.
All you need to do is update your model class, and new entities will be saved with the structure (fields) of the new entity.
Old entities and indexes, however, are not automatically updated. They still have the same fields as they had when they were originally written to the datastore.
There's two ways to do this. One is to make sure your code can handle situations where your new properties are missing, ie make sure no exceptions are thrown, or handle the exceptions properly when you're missing the properties.
The second way is to write a little function (usu a mapreduce function) to update every entity with appropriate or null values for your new properties.
Note that indexes are not updated unless the entity is written. So if you add a new indexed property, old entities won't show up when you query for the new property. In this case, you must use the second method and update all the entities in the datastore so that they are indexed.
I've created a database using the EF4 model-first approach. In my model, there's an N-to-M relationship between two entities:
I've filled my database with some dummy data, including 3 records of type Diagnosis and 3 records of type TreatmentSchema and associations between them. Here's the code snippet I used to do this:
using(var container = new SmartTherapyContainer()) {
var diagnosisA = new Diagnosis() { Id = Guid.NewGuid(), Name = "Diagnosis A" };
var diagnosisB = new Diagnosis() { Id = Guid.NewGuid(), Name = "Diagnosis B" };
var diagnosisC = new Diagnosis() { Id = Guid.NewGuid(), Name = "Diagnosis C" };
container.Diagnoses.AddObject(diagnosisA);
container.Diagnoses.AddObject(diagnosisB);
container.Diagnoses.AddObject(diagnosisC);
var schemaA = new TreatmentSchema() { Id = Guid.NewGuid(), Name = "Schema 1" };
var schemaB = new TreatmentSchema() { Id = Guid.NewGuid(), Name = "Schema 1" };
var schemaC = new TreatmentSchema() { Id = Guid.NewGuid(), Name = "Schema 1" };
container.Schemas.AddObject(diagnosisA);
container.Schemas.AddObject(diagnosisB);
container.Schemas.AddObject(diagnosisC);
diagnosisB.TreatmentSchemas.Add(schemaA);
diagnosisC.TreatmentSchemas.Add(schemaA);
diagnosisC.TreatmentSchemas.Add(schemaB);
diagnosisC.TreatmentSchemas.Add(schemaC);
container.SaveChanges();
}
I verified that the associations are indeed stored in the reference table created through EF4's mapping. However, when I retrieve a Diagnosis via the container.Diagnoses collection later, its .TreatmentSchemas collection is always empty.
I tried debugging into the EF4-generated code and all it does is lazily create said collection, but it doesn't fill it with the associated objects. Ayende's Entity Framework Profiler shows no queries being generated at all when the property is accessed, which leads me to believe that I'm doing something wrong here.
How can I get a list of the associated TreatmentSchemas?
Navigation properties are not loaded by default. You must use either eager loading or lazy loading but because you are using self tracking entities your choice is only eager loading because STEs don't support lazy loading. So if you want to get Diagonstic instance with all related TreatmentSchemas you must call:
var diagnosis = context.Diagnoses.Include("TreatmentSchemas").FirstOrDefault();