How to query the latest inserted item in with Morphia
And how to get the size of a collection?
It depends what you mean by latest, but if you have a Date field with the creation date then you would do this:
T latest = ds.find().sort("-dateCreated").get();
And this would get you the count:
int count = ds.getCount(Class.class);
When you save a document, the find method returns a object Key.
If you want to know your object with the key that morphia insert in database, just get the key and put in the object parameter. I'm doing this way in my application.
To sort can you just pass .sort("dateCreated") to sort in ASC, or .sort("-dateCreated") that sort for the DSC way.
Related
I'm having a lot of trouble trying to update a single (record) object in my database.
context['eval_list'] = Evaluering.objects.update(eval_relationer_id=self.kwargs.get('pk'))
I use objects.update, but it updates ALL my objects fk. How do I achieve only updating one object? I have also tried this:
context['eval_list'] = Evaluering.objects.update_or_create(eval_relationer_id=self.kwargs.get('pk'))
But this creates a new object and does not update the record that I want to update. I know why it creates a new objects, and it is because the FK I'm trying to update is null. Surely, there must be a way to only update and not create a single record? What am I missing here?
I tried adding a filter, but it feels redundant? I tried this:
context['eval_list'] = Evaluering.objects.filter(eval_relationer_id=self.kwargs.get('pk')).update(eval_relationer_id=self.kwargs.get('pk'))
I did consider trying to create an ID of the FK instantly and not later on, but I couldn't really get that to work, but if I created an ID then the update_or_create would work because an ID would exist already, BUT I cannot believe that I can't update a single object without create?
If creating the ID earlier on is the only work around, I will have to figure out how.
MyModel.objects.filter(pk=some_value).update(field1='some value')
The filter gets your object (returns the Queryset with only that object), then the update changes some other field that is not the PK to whatever you want.
In your case probably something like this:
context['eval_list'] = Evaluering.objects.filter(eval_relationer_id=self.kwargs.get('pk')).update(some_attribute='some value')
After help from #Hanny I've figured out was going wrong.
I was trying to filter by the eval_relationer_id, when I should have been filtering by the evaluation pk and getting that specific PK. Otherwise I would be updating ALL the values which is not what I wanted.
So by filtering by pk:
filter(pk=self.kwargs.get('pk'))
And updating by the attribute / fk that I want to update
update(eval_relationer_id=self.kwargs.get('pk'))
This is the end-result:
context['eval_list'] = Evaluering.objects.filter(pk=self.kwargs.get('pk')).update(eval_relationer_id=self.kwargs.get('pk'))
I use sorted set data type in Redis.
I add data with command zadd. Adding data is JSON format.
How I can change value in this sorted set by score?
I need get JSON value and change one field and after update this sorted set.
I tried to add againg data with the same score, but I get dublicates
It's simple!
ZREM key data;
ZADD key score newdata;
You simply cannot UPDATE an element in SET structure. It's not possible by definition! Like I cannot EAT a cup of water, I can only DRINK it ^_^
Reply if you have any further problems.
By the way, I don't know your application need, but I have a strong feeling that SORTED SET is unsuitable for your application senario.
One does not update a set's (sorted or not) members. You'll have to remove the old member and add the new (updated JSON) in its place with the relevant score. You could wrap this in Lua or a WATCH/MULTI/EXEC block for atomicity.
You could remove first by score if you're using the list as a key (score) => value store.
ZREMRANGEBYSCORE key score score
ZADD key score data
I have a properly set up NSFetchedResultsController that returns all the objects I want. However, I want to set it up so that it returns only objects that are unique by ID value, because there are several duplicate objects that have the same ID but other different properties in the DB.
I tried to set it up like the documentation says and ended up with this result:
fetchRequest.returnsDistinctResults = YES;
fetchRequest.propertiesToFetch = #[#"eventCategoryID", #"eventName", #"eventID", #"eventPeopleCount", #"eventPrice", #"eventCrewCount", #"eventStartDateTS", #"eventImageURL", #"shouldShowFriends", #"isLiked"];
However they say that you have to use this property only with NSDictionaryResultType. And moreover, it gives me a strange crash.
What are the approaches to make objects returned by NSFetchedResultsController filtered so as to have a custom unique property like ID?
So you want to find all the objects where the ID property is unique? Then yes, NSDictionaryResultType is the way to go. I think you will need two fetches, though.
First, build a fetch request to retrieve all of the ID values, and a count for each value. Use NSExpressionDescription to apply the #count function to your results, and use an NSDictionaryResultType. Group by ID. A step by step example is at http://mattconnolly.wordpress.com/2012/06/21/ios-core-data-group-by-and-count-results/.
Now you'll have a dictionary with keys for ID and the count of that ID. Filter that dictionary so that you only have IDs where the count was 1, and extract just those ID values. Now you can build a second fetch request based on ID IN ... your array of singleton IDs.
It might be possible to build a more complicated original fetch request to return only the count=1 IDs.
I currently have a Redis Set and I'm not sure if this is possible but thought I'd ask just in case someone knows of a way around it. Is it possible to add a date of when a new element for a key is created when using 'SADD' command?
I've tried searching the web but I can't find anything, which tells me its not possible. Is there another way of doing this? Something like the below and then being able to get the date the element was created?
SADD my:key:string "1000", Time.now
Use a Sorted Set instead of a Set. Using the timestamp as a score will allow you to retrieve your values ordered by date, values for a given date, values after of before a given date, etc. Or of course just the date for the value.
ZADD myzset 12345678 "my value"
In order to set the date of an element save the element in form of json :
SADD mset "{value:1000,created:18-03-2014}"
So when you retrieve the element its creation date will be also be retrieved.
According to the REST philosophy, a PUT request should update the resource at a URL if it exists, and create it if it doesn't exist. So in other words, if I use the following URL:
PUT http://server/item/5
If an Item exists with an ID of 5, it will be updated. If an Item doesn't exist with an ID of 5, a new Item will be created with an ID of 5.
However, I'm using NHibernate for persistence, and I mapped my IDs as Identity. This means that no matter what value I assign the ID, NHibernate will replace it with its own when I save a new Item.
How do I get NHibernate to save an Item with the ID that I assign it, without changing the ID mapping to Assigned?
If you use Identity, the DB won't allow you to enter a value.
That said, if your DB has some special syntax to allow inserting with explicit values in Identity fields, you can implement your own generator, which I guarantee will be error prone, hard to debug, and not very useful. But it's possible.
Study everything in https://nhibernate.svn.sourceforge.net/svnroot/nhibernate/trunk/nhibernate/src/NHibernate/Id and start creating your Frankenstein :-)