i want to pass autogenerated id to 'Name/id' at datastore.can anybody help me for this? here is my code :
String Id = "" // i want autogenerated value
profile = new Profile(Id, displayName, mainEmail);
Id must be autogenerated here. so how to pass it?
ObjectifyFactory has a method #allocateId(). You ca find an example usage in this question.
Essentially you do
new ObjectifyFactory().allocateId(Profile.class).getId()
In case you do not need the id right away I would not use this approach. Just annotate the id with #Id, set it to null and save the entity. When you do a ofy().save().entity(...).now() it will return a Key that contains the new id.
Allocating ids via allocated id still performs a datastore request. It will allocate a block of ids of which you will use just one in this case. Use it if you must, don't, if you don't have to.
Related
Please I want to give custom IDs to my database objects in Firebase but I don't now how to do it. Firebase creates default IDs for database objects which I don't want. I want to be able to assign my own IDs to objects or the child nodes of in the database for unique identification.
Most likely you're adding the items to the database with something like:
ref.push().set("my value");
This generates a new unique key under ref and sets your value on it.
If you want to use you own key/name for the child location, add the item with:
ref.child("my key").set("my value");
You cannot customize ID of firebase object, but you can create another field with ID role.
ref.child("my_id").set("customize_id");
after that, using "Filter by key" to get exactly your object you want.
In our case: We need to have a user_id type Int and auto-increase, so we can't use default _id of firebase object, we create user_id ourself to solve this problem.
Problem: Whenever I add an order to the orders array, an additional nested array element(-KOPWA...) gets added. I wouldn't mind except, I don't know how to access that nested string to access it's child nodes.
Example of database node for users below:
firebase.database().ref('users/'+userIdState+'/orders/'+<<unique numbervariable>>).push({
"order":{"test":"product","quantity":2}
});
I'm using the above code to push new json objects with a unique number to the firebase array. Still the nested array with the weird strings gets generated.
Can anyone help me understand how to either: create my own nested array with my own unique string or how to access the nested string that gets generated from firebase so I can access it's children nodes.
Multiple instances of nest arrays will be generated by users.
Any help is much appreciated.
Thanks,
Moe
You're experiencing this behaviour because Firebase's push is not the same as an array push. I recommend reading this article to understand how it works.
As for a solution, you can simply change push to set in your code. This will create the structure you were (presumably) expecting, that is
1:
order:
...
This is however potentially unsafe, if you allow concurrent writes (i. e. if the "unique number" in your example is not always unique).
Afaik Firebase recommends using push to safely create collections/"arrays". You can retrieve the generated key by calling the key property on the reference returned by push. Like this:
var ref = firebase.database().ref('users/'+userIdState+'/orders/'+<<unique numbervariable>>).push({
"order":{"test":"product","quantity":2}
});
var generatedKey = ref.key; // the value you're looking for
If you decide to use it, you can probably just drop the order number you have right now and just use the generated one.
Please explain above question with example scenario I am confusing which is best.
If you to fetch a specific object based on keyword or any identity in list then you have to iterate the list get object and compare with its values
In map you can directly create key value pair..you can pass key and get the value.
ex:
A object user is present which has several properties one of them is user code
Now if you have list of user object then you will fetch one by one user object and compare the code of each user...but in map you can directly store user object with user code as key pass the key and get the desired object
map.get("key");
but if you requirement is not based on key type access better to use list.. example as you to just display list of items or you have to perform sublisting.
Too broad question, but will try to shorten it:
When you have to get the value based on key (key can be anything) then you go for hashmap. Consider a telephone directory where you go to appropriate name and search for person's name to find his number.
While if you have similar object's and want to store them somehow and later on retrieve it say by index or traverse them one by one then you go for list. So if your task is to find employees older than age 50 yrs, you can just return a list of employees who are older than 50.
When using mini-mongo to insert documents into a collection a user could pass the _id field and that _id will be set on the document as long as it is a string or an ObjectID (and doesnt collide with an existing id). That doesnt seem sensible to me. I want the _id of the documents to be generated by the server, always, so I dont end up with a db where both strings (potentially of different length) and ObjectIDs exist as _ids.
Is there a simple way to achieve this? Right now the best I can think of is checking the id in a deny rule (where the _id will either already be set by Meteor or be the value the user provided) and if it's not the type/length I want it to be I change it, but that also requires checks to avoid duplicate ids. Not too difficult but seems overly complex for something as basic as this.
Reviewing my response, here's your answer:
Deny all inserts. Create a method, collectionInsert(document), that checks if an _id field is specified.
Meteor.methods({
collectionInsert: function(document) {
if (document && document._id) {
delete document._id;
}
// Returns the _id generated
return collection.insert(document);
}
});
In today's versions of meteor (0.8 and higher) the server and client generate the same _id in this case anyway. But using this method should ensure that the client simulates the field insert and that the server's version of the generated _id is authoritative.
The correct way to do this is probably to do the insert in a Meteor.method. You can't trust client-generated IDs in high-risk applications, so using collection#insert with allow/deny hooks isn't sufficient. Instead, call a method to do the insert, which will always create a server-generated ID.
You can still stub this method on the client, for latency compensation, but the inserted ID will be overwritten when the server call returns.
Assume:
class Contacts(db.Model):
first_name = StringProperty()
last_name = StringProperty()
phone_number = PhoneNumberProperty()
new_contact = Contacts(first_name="Homer", last_name="Simpson", phone_number = 1234566)
new_contact.put()
I am new to GAE Datastore, but per GAE Datastore Docs (see below if interested), i can't modify a single property of the entity (eg, phone_number). I have to re-declare all properties, and then put() the new entity. Omitting previously-declared properties from the new entity results in them being declared as None. Is there a workaround for this -- such as manually overriding the key-name generation function -- so the entity maintains the same key?
#
from GAE Datastore Docs:
To update an existing entity, modify the attributes of the object, then call the put() method. The object data overwrites the existing entity. The entire object is sent to the datastore with every call to put(). Note: The datastore API does not distinguish between creating a new entity and updating an existing entity. If the object's key represents an entity that exists, calling its put() method overwrites the entity.
that's not true. You need to have a way to get the contact you want and you can update just that. Using the keyname is only one way to do it. If you know the ID of filter a query to only get one entity, you can update a field from it and the put() to update it.
You could have something like:
query = Contact.all().filter('first_name', 'john').filter('last_name', 'doe')
for contact in query:
contact.phone_number = 498340594834
contact.put()
Note that that code would update any contacts with that name to that phone number. If there is more than one with that name, both are updated. Using a keyname can prevent that but you have to create more complex keys since only the first and last name might colide.