How to reference sibling id in mongodb without printing that particular sibling - mongodb

enter image description here
I'm trying to use mongosh to retrieve the details of the other siblings, i.e Child2 and Child3 by referencing the child1 document.
What mongosh condition should I use to get this and not the child1 and father document?
I tried the aggregate method and the find method, I tried to apply the logic of using the parent_id, which is common among the other siblings, but it prints the child1 id as well. Help!

I figured it out after some trial and error and I used the $ne function. So according to the image, the answer should be:
db.family.find({parent_id: "1", _id:{$ne:2}})

Related

Liferay Service Builder - is there a recommended way to describe a self reference relation in service.xml?

I want to describe a self reference relation in a Liferay entity. Is there a recommended way to do this ?
I want to do this in order to describe a hierarchy.
At the moment, I just added a new column which I called "parentId" and I will save there the ID of the row that will be the parent of this one. If I use the "Diagram view", from within eclipse, if I draw a self reference relationship it adds a new row that duplicated the name of the ID: for example, suppose I want to describe an employee hierarchy - I have an Employee entity on which I add the default fields; one of these fields is the employeeId field which will be also the primary key. Now, if I draw a self reference relationship, the IDE will add another field entry that will be named the same way (eg. employeeId)
There is no official recommended way to do it, as you will have to add your own logic in your code to handle it.
In case you want to follow the convention used in Liferay code, Liferay code usually represents the hierarchy using a column called parent<primaryKey> and sometimes an auxiliary column called treePath to store the hierarchy path of the element, see:
https://github.com/search?q=repo%3Aliferay%2Fliferay-portal+filename%3Aservice.xml+parent&type=Code
https://github.com/search?q=repo%3Aliferay%2Fliferay-portal+filename%3Aservice.xml+treePath&type=Code
About the treePath column, service builder will add to the java class some methods (buildTreePath and updateTreePath) that will help to populate it, see the service builder templates: https://github.com/liferay/liferay-portal/blob/11e6081f96abb6bf299369519434c1eafa0658e3/modules/util/portal-tools-service-builder/src/main/resources/com/liferay/portal/tools/service/builder/dependencies/extended_model_base_impl.ftl#L65-L115
This column makes easier to get all the parent elements of the hierarchy, just split it by the / char and you will get all the primary keys of the ancestors.

Getting records through parentID from CategoryCollection in Typo3

I am using CategoryCollection to get the records of a specific category ID, but the problem is it only loads the exact category for e.g I have parent > child and I have attached child category ID to a record and I select child category, then it shows me the record fine, but if I select parentID, then it does not show the child category record.
$collection = \TYPO3\CMS\Frontend\Category\Collection\CategoryCollection::load(
$categoryID,
true,
'tx_myextension_table_name',
'categories'
);
Is there any built-in way to get the records of all child category if I select parent ID from CategoryCollectionor do I have to write something custom for that?
Unfortunately there is no built-in solution for complex selections like this. You will indeed need to write your own logic which could work like this:
Find categories whose parent is your category
Repeat this recursively for every category found until you don't find any children for each category anymore
Do a custom IN() query with the list of category UIDs
If you have deep category trees, the list of category UIDs could be put in a custom cache. You can use the root category UID or a hash thereof as key. These cache entries should be tagged with sys_category. Alternatively you can add a sys_category_<uid> tag for every category UID in your list. This ensures that whenever something changes about one of the categories, the cache entries are dropped and you can rebuild the list.

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.

parent child relationship query mistake in MongoDB in Action book

I am suspecting the method defined to get siblings in the book MongoDB in Action
Every category keeps parent_id that is the parent category id. So sibling categories should have common parent_id.
But to query siblings for a particular category (let's say category['_id'] = C5) book says following:
siblings = db.categories.find({'parent_id': category['_id']});
Returned cursor is obviously not siblings of C5, they are child of C5.
As per my understanding query should be
siblings = db.categories.find({'parent_id': category['parent_id']});
Please comment.
You are correct, and this is fixed in the second printing of the book. See the errata here:
http://manning.com/banker/excerpt_errata.html

Repositories and getting new values for an aggregate entities / value objects

I'm just starting out with DDD and have built a repository pattern using EF code first which so far is working very well. Now say I have an aggregate root call Animal which has an entity called Status.
Now if I need to populate a drop down list with Status objects, or I need to replace the Status object in animal with a new one. How should I access the Status collection. In this case Status is not an aggregate root and only has meaning by association with Animal, it will have an identity though.
Should I either create a new Repository for Status by making it an aggregate root (it just one of many such things so this might get out of hand), or do I allow access the Status collection via the AnimalRepository with something like GetStatusByID or GetAllStatuses?
This same question could equally apply to value objects such as color, breed, sex etc.
This sort of stuff I'd treat as lookup/reference data; I've found this answer useful in the past: Loading a Value object in List or DropdownList, DDD
But basically I'd have a separate repository.