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
Related
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}})
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.
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.
I'm working with Breeze and love it but came across a stickler or two and am hoping to get a little help with one of them.
I'm using EF and SQL on the back-end of a database first model and have a one to many relationship with a Parent and Child table. When retrieving data, no matter what I try, it doesn't filter the data correctly when using the child id. Here's the code:
Client Code:
var query = breeze.EntityQuery.from("Parent")
var predicateChild = new breeze.Predicate('CHILD', 'any', 'CHILD_ID', '==', childId);
query = query.where(predicateChild );
var promise = collectionsManager
.executeQuery(query)
.then(querySuccess)
.catch(queryFailed);
Server Code:
[HttpGet]
[BreezeQueryable(MaxExpansionDepth = 5)]
public IQueryable<PARENT> Parent()
{
return _contextProvider.Context.PARENT;
}
The query does work, it finds the proper parent (so it seems the childId is being used) but returns all children, not just the one given in the predicate.
I've tried every way I can to get the results I want but I'm sure I'm missing something either in the server code or the EF layer.
In an effort to get this to work, I changed the server code to use the child table instead of the parent. If navigation is set in EF from child to parent, this works, but that presents a circular reference when stringifying the data later downstream (and I need to do this).
Any help is greatly appreciated.
Your query has a filter on PARENT. It does not filter related CHILD entities, which seems to be the intent of your question.
Specifically you're query filters for a PARENT which has at least one CHILD whose CHILD_ID == childId. It does not return any CHILD entities as written. If you added an expand clause you would get all of the PARENT entity's children, not just the one with CHILD_ID == childId.
EF and OData do not support filtering of entities returned via an expand. Sorry about that. I wish they did.
This question comes up a lot. It's discussed in many S.O. answers, most recently this one.
I have a self referencing table named categories that has a parentcategoryid column that is nullable.
When I added the table to the entity designer it created two navigation properties for this relationship and I named one ParentCategory (the zero or 1 nav prop) and the other I named SubCategories (the * many nav prop).
Everything works great except when I go more than one level deep it doesn't pick up the deeper levels.
So I get all the Category.SubCategories but I don't get the categories under the subcategories.
Am I missing something? starting to think I should have stuck with NHibernate. Shouldn't the deeper levels get lazy loaded?
return from c in _entities.ContentCategorySet.Include("SubCategories")
where c.ParentCategory == null
orderby c.Importance, c.Title
select c;
That's how I would Imagine the SubCategories property to behave.
Level 1
++ Level 2
++ Level 2
++ ++ Level 3
Where SubCategories property of Level 1 only returns Level 2 items. Then to get to Level 3 you would access the consecutive Level 2 items, in a recursive method.
OK, At least part of the problem is
where c.ParentCategory == null
When I remove that I get the deeper levels but then have subcategories on the top level. I guess I can just filter them out after the fact.