Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pymongo树结构获取节点子体_Python_Mongodb_Pymongo - Fatal编程技术网

Python Pymongo树结构获取节点子体

Python Pymongo树结构获取节点子体,python,mongodb,pymongo,Python,Mongodb,Pymongo,我想将这个示例移植到Python 以下是获取所有节点子体的原始示例: var descendants=[] var stack=[]; var item = db.categoriesPCO.findOne({_id:"Cell_Phones_and_Accessories"}); stack.push(item); while (stack.length>0){ var currentnode = stack.pop(); var children = db.catego

我想将这个示例移植到Python

以下是获取所有节点子体的原始示例:

var descendants=[]
var stack=[];
var item = db.categoriesPCO.findOne({_id:"Cell_Phones_and_Accessories"});
stack.push(item);
while (stack.length>0){
    var currentnode = stack.pop();
    var children = db.categoriesPCO.find({parent:currentnode._id});
    while(true === children.hasNext()) {
        var child = children.next();
        descendants.push(child._id);
        stack.push(child);
    }
}


descendants.join(",")
//Cell_Phones_and_Smartphones,Headsets,Batteries,Cables_And_Adapters,Nokia,Samsung,Apple,HTC,Vyacheslav
Python版本与此类似:

def descendants():
    descendants = []
    stack = []

    item = db.electronics.find_one({'_id': "Cell_Phones_and_Accessories"})

    stack.append(item)

    while(len(stack) > 0):
        currentNode = stack.pop()

        children = db.electronics.find({'parent': currentNode["_id"] })

        while(next(children, None)):
            child = next(children, None)
            descendants.append(child['_id'])
            stack.append(child)

    print(descendants)
但正如您从输出中看到的,缺少一些子体

[‘电池’、‘手机’、‘智能手机’、‘三星’、‘HTC’]


在while循环中调用next两次,因此第一个next()会使您跳过这些项,请尝试以下代码

def descendants():
    descendants = []
    stack = []

    item = db.electronics.find_one({'_id': "Cell_Phones_and_Accessories"})

    stack.append(item)

    while(len(stack) > 0):
        currentNode = stack.pop()

        children = db.electronics.find({'parent': currentNode["_id"] })

        for child in children:
            descendants.append(child['_id'])
            stack.append(child)

    print(descendants)
def descendants():
    descendants = []
    stack = []

    item = db.electronics.find_one({'_id': "Cell_Phones_and_Accessories"})

    stack.append(item['_id'])

    while stack:

        children = db.electronics.find({'parent': {'$in':stack}})
        stack = []
        for child in children:
            descendants.append(child['_id'])
            stack.append(child['_id'])

    print(descendants)
上面是对代码的更正,但您可以通过以下代码减少数据库调用

def descendants():
    descendants = []
    stack = []

    item = db.electronics.find_one({'_id': "Cell_Phones_and_Accessories"})

    stack.append(item)

    while(len(stack) > 0):
        currentNode = stack.pop()

        children = db.electronics.find({'parent': currentNode["_id"] })

        for child in children:
            descendants.append(child['_id'])
            stack.append(child)

    print(descendants)
def descendants():
    descendants = []
    stack = []

    item = db.electronics.find_one({'_id': "Cell_Phones_and_Accessories"})

    stack.append(item['_id'])

    while stack:

        children = db.electronics.find({'parent': {'$in':stack}})
        stack = []
        for child in children:
            descendants.append(child['_id'])
            stack.append(child['_id'])

    print(descendants)