Mongodb query problem;

  mongodb, question

Data structure

{
 "_id" : ObjectId("59e72a88d416aa6ab8f79d7b"),
 "category_name": "oil",
 "parent_id" : "0",
 "__v" : 0
 }
 
 /* 2 */
 {
 "_id" : ObjectId("59e72acad416aa6ab8f79d7c"),
 "category_name": "meat",
 "parent_id" : "0",
 "__v" : 0
 }
 
 /* 3 */
 {
 "_id" : ObjectId("59e85b76813a3011280b1186"),
 "category_name": "sesame meat",
 "parent_id" : "59e72acad416aa6ab8f79d7c",
 "__v" : 0
 }
 
 /* 4 */
 {
 "_id" : ObjectId("59e85b84813a3011280b1187"),
 "category_name": "pork",
 "parent_id" : "59e72acad416aa6ab8f79d7c",
 "__v" : 0
 }
 
 /* 5 */
 {
 "_id" : ObjectId("5a15027971d44b046f83d605"),
 "category_name": "beef oil",
 "parent_id" : "59e72a88d416aa6ab8f79d7b",
 "__v" : 0
 }
 
 /* 6 */
 {
 "_id" : ObjectId("5a1502d871d44b046f83d606"),
 "category_name": "peanut oil",
 "parent_id" : "59e72a88d416aa6ab8f79d7b",
 "__v" : 0
 }
 
 /* 7 */
 {
 "_id" : ObjectId("5a154dd83af2940ba6727a39"),
 "category_name": "soybean oil",
 "parent_id" : "59e72a88d416aa6ab8f79d7b",
 "__v" : 0
 }

This is my query code; The result is not available, is it suitable to use combination function? Or is there something wrong

db.getCollection('categoties').group({
 keyf:function(doc){
 if(doc.parent_id==0){
 return {id:doc._id,name:doc.category_name}
 }else{
 return {id:doc.parent_id}
 }
 },
 reduce:function(obj,prev){
 if(obj.parent_id!  =0){
 prev.items.push(obj);
 }
 },
 initial:{items:[]}
 })
 # The results are as follows:
 
 [
 {
 "id" : ObjectId("59e72a88d416aa6ab8f79d7b"),
 "name": "Oil",
 "items" : []
 },
 {
 "id" : ObjectId("59e72acad416aa6ab8f79d7c"),
 "name": "Meat",
 "items" : []
 },
 {
 "id" : "59e72acad416aa6ab8f79d7c",
 "items" : [
 {
 "_id" : ObjectId("59e85b76813a3011280b1186"),
 "category_name": "sesame meat",
 "parent_id" : "59e72acad416aa6ab8f79d7c",
 "__v" : 0
 },
 {
 "_id" : ObjectId("59e85b84813a3011280b1187"),
 "category_name": "pork",
 "parent_id" : "59e72acad416aa6ab8f79d7c",
 "__v" : 0
 }
 ]
 },
 {
 "id" : "59e72a88d416aa6ab8f79d7b",
 "items" : [
 {
 "_id" : ObjectId("5a15027971d44b046f83d605"),
 "category_name": "beef oil",
 "parent_id" : "59e72a88d416aa6ab8f79d7b",
 "__v" : 0
 },
 {
 "_id" : ObjectId("5a1502d871d44b046f83d606"),
 "category_name": "peanut oil",
 "parent_id" : "59e72a88d416aa6ab8f79d7b",
 "__v" : 0
 },
 {
 "_id" : ObjectId("5a154dd83af2940ba6727a39"),
 "category_name": "soybean oil",
 "parent_id" : "59e72a88d416aa6ab8f79d7b",
 "__v" : 0
 }
 ]
 }
 ]

The result I want is this:

[
 {
 "id" : ObjectId("59e72a88d416aa6ab8f79d7b"),
 "name": "Oil",
 "items" : [
 {
 "_id" : ObjectId("5a15027971d44b046f83d605"),
 "category_name": "beef oil",
 "parent_id" : "59e72a88d416aa6ab8f79d7b",
 "__v" : 0
 },
 {
 "_id" : ObjectId("5a1502d871d44b046f83d606"),
 "category_name": "peanut oil",
 "parent_id" : "59e72a88d416aa6ab8f79d7b",
 "__v" : 0
 },
 {
 "_id" : ObjectId("5a154dd83af2940ba6727a39"),
 "category_name": "soybean oil",
 "parent_id" : "59e72a88d416aa6ab8f79d7b",
 "__v" : 0
 }
 ]
 },
 {
 "id" : ObjectId("59e72acad416aa6ab8f79d7c"),
 "name": "Meat",
 "items" : [
 {
 "_id" : ObjectId("59e85b76813a3011280b1186"),
 "category_name": "sesame meat",
 "parent_id" : "59e72acad416aa6ab8f79d7c",
 "__v" : 0
 },
 {
 "_id" : ObjectId("59e85b84813a3011280b1187"),
 "category_name": "pork",
 "parent_id" : "59e72acad416aa6ab8f79d7c",
 "__v" : 0
 }
 ]
 }
 ]

Yoursparent_idFields and_idThe data format of the fields should be consistent so that the design is reasonable:

The following answer is yoursparent_idFields and_idIf the data cells of the field are consistent:
Using aggregate functionaggregate

db.getCollection('test').aggregate([
 {$match:{"parent_id" :"0"}},
 {$lookup:{from:'test',localField:'_id',foreignField:'parent_id',as:'items'}}
 ])