MongoDB storing and querying child objects - mongodb

I've two different object with same father. I want to store them in the same collection, but I want to be able to retrieve each object separately.
for example if these are my objects:
I want to retrieve all of FirstChild objects without retrieving any SecondChild Object.
Is there any way other than adding a type field to the father object, to retrieve them?

Assuming first child and second child are different types stored in different fields of the father object (father is a composition of first and second child)
datastore.find(FatherObject.class).retrievedFields(false,"secondChildField")
will get everything except secondChildField or
datastore.find(FatherObject.class).retrievedFields(true,"firstChildField")
will bring only firstChildField.

When you create your query, pass in the class reference of the type you want: datastore.createQuery(SecondChild.class). Morphia, by default, tracks the class type of the document so it can filter by that type.

Related

How can I reorder elements in Realm LinkingObjects?

I have an Item data class, and each Item has one parent and many children Items, which are LinkingObjects from the parent property.
A simplified version:
#objcMembers class Item: Object {
dynamic var title: String = ""
dynamic var parent: Item?
let children = LinkingObjects(fromType: Item.self, property: "parent")
}
I need to be able to reorder the children, and persist that change. It seems that because LinkingObjects is a container type this wouldn't be possible? In this case, how would you recommend I approach this--should I change children to a normal List<Item> and just manually update the realm two ways every time? Is there a better solution?
The order in which the children appear matters, because users will want to choose their own ordering based on which items they want to see first.
The important part here is that you want to control the 'order in which the children appear'. If you can recognise the difference between the 'model' of the data and the 'view' of the data then you can work out the answer. Remember that the Realm model representation is not under your control, and you don't need it to be. How it's stored in the Realm database is not your problem.
In reality, the results of the LinkingObjects field when you request it will probably be the result of a database query, and not a straight pull from a table. So the order of objects in the field may be inconsistent, or adding further linked objects may cause the extra item to appear anywhere in the results.
As you say, the important thing is the order in which the children appear. And this can be controlled through a query. The LinkingObjects type allows you to query directly, i.e. you can call:
item.children.sorted(byKeyPath: "title")
One suggestion would be to add an extension to your model type to handle the sorting for you, providing an accessor for each sort type you require, e.g.:
extension Item
{
var childrenSortedByTitle: Results<Item>
{
return children.sorted(byKeyPath: "title")
}
}
Or you could make that a function with an ascending boolean parameter to use in the query. Or you could add a second query for ascending/descending. And you can add extra queries for each other parameter you would like to sort by. Or you could define an enum of sort fields and pass that it as an argument. Whichever of these methods works best for you. But it is definitely worth hiding the query details in the class extension itself (IMHO).

Locust: how to share data between two different types of users

I have a system where there are two types of entities - parent and child.
When a parent goes into a particular state, it creates one or more child entities and the child entities then go through their state changes independent of each other and the parent. The main thing to note is that when a parent spawns off a child, he also assigns an id to it and the child has to use that id while going through the state changes.
So, in my locustfile, I created two different classes as below:
class ParentLocust(Locust):
task_set = ParentTaskSet
class ChildLocust(Locust):
task_set = ChildTaskSet
My question is that when an instance ofo the ParentLocust issues the REST call which leads to the creation of a child entity, how can the id of the child entity be conveyed to a ChildLocust instance?
My first thought was to use a global dictionary, where the key is the parent_id and the value is a list containing the different child_id. Now, an instance of the ChildLocust gets assigned a random child_id by doing a random lookup in the dictionary and then removes it from the dictionary as well (so that no other ChildLocust gets assigned the same child_id). But I am not sure if this is correct, because there are two many instances (parent as well as child) accessing the same dictionary at the same time.

Realm Swift inverse relationships many-to-many

I'm currently trying to work out the best way to architect my realm objects for ease of retrieval.
I have 2 objects tags and object there are multiple tags and each one might contain many object. Similarly each object could have multiple tag associated with it
Ideally selecting a single tag should retrieve all object that have at least that one tag (but could obviously have multiple)
would my models be specified as
class Tag: Object {
let objects = List<Object>()
}
class Object {
let tags = List<Tag>()
}
I don't think I need to use an inverse relationship here or should I? Choosing a Category I should be able to just retrieve a list of all object references regardless, but then maintaining and updating the references to an object might be difficult here? I.e a user selects tag 'A' then updates the first object to also include tag 'B' I would need to update the object in the List for Tag A, then add a new item to the list for Tag 'B' and finally update the actual Object itself to include Tag 'B' in it's list of tags.
Just to be clear an Object will only ever display and allow editing of it's Tag objects. But the Tag object itself will need to know what Object's are applicable to it.
However it feels like I will have to do multiple updates when ideally I'd like to minimise this effort. Can anyone recommend a better way to do this? Or is there no way around this due to the limitations of Realm?
This is exactly what LinkingObjects is for. Changing the objects property in Tag to let objects = LinkingObjects(fromType: Object.self, property: "tags") will make it automatically update whenever a tag is added to an object.

entity framework wrong order of child object after some child objects detached

my problem is after first child object detached and then i add another child object to my parentObj the order of attached child is not correct
, my code look like this:
parentObj= new TparentObj();
firstChildObj=new Tchild1();
secondChildObj= new Tchild1();
thirdChildObj=new Tchild1();
parentObj.Tchild1.add(firstChildObj);
parentObj.Tchild1.add(secondChildObj);
// now parentObj.Tchild1.first()==firstChildObj return true
///then for some reason
parentObj.Tchild1.remove(firstChildObj);
db.Entry(firstChildObj).State = EntityState.Detached;
// now i add third childObj
parentObj.Tchild1.add(thirdChildObj);
//// now parentObj.Tchild1.first()==thirdChildObj return true!!
after saved db the result in database is Correct;
but how can i get list of childObj in order they added?
Entity Framework by default uses HashSets for its collections. HashSet doesn't take ordering into account.
You shouldn't rely on the ordering of elements for it. The current implementation seems (as you are experiencing) to add the element on the first unused position (in your case, the removed one), but this is an implementation detail and you shouldn't rely on it.
About HashSet, the MSDN says about it (bold is mine):
The HashSet class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
and
A HashSet collection is not sorted and cannot contain duplicate elements. If order or element duplication is more important than performance for your application, consider using the List class together with the Sort method.

NSMutableArray of NSString in CoreData(iPhone

I have a class, which describe an object for parsing XML. This class contains NSStrings and NSMutableArrays of NSStrings.I want to store parsed data using CoreData. How should I write CoreData model to store such objects if there is no such data type like NSMutableArray? Is there any way?
You can create two entities, I'll call them Array and String. Array has one relationship, strings, which is a one-to-many relationship with the String entity. The String entity then has one attribute, I'll call it string too, which actually is a string and contains the value you need. This will allow you to store them as a set, if the order is important add another attribute to the String entity called order and make it a number.
There isnt an array data type, but u can box the string in a core data object (lets call it A for the remainder of the post). Then in the object (B) which you want to have the array of string (that persists) in just make a to-many relationship with the object A which contains the string, then youll have a Set of string on the object B which you can turn into an array and do with what you like. hope that helps