简体   繁体   English

循环在MongoDB中不起作用

[英]Loop doesn't work in MongoDB

I wrote script for search anagrams in my Mongo database: 我在Mongo数据库中编写了用于搜索字谜的脚本:

print("Search anagrams");

var input = db.dbName.find();
var i = 0;

input.forEach(function(word) {
    var j = 0;
    var alphabetical = word.name.split("").sort().join("");

    input.forEach(function(worter) {
        if (i != j) {
            if (alphabetical == worter.name.split("").sort().join("")) {
                print(word.name + " : " + worter + " - " + i + ", " + j);
            }
        }
        j++;
    });
    i++;
});

but the first loop doesn't work. 但是第一个循环不起作用。 Do you have any idea why? 你知道为什么吗? Sample record from db: 来自db的样本记录:

{ "_id" : ObjectId("5491d807dacc815b3043ae24"), "name" : "monki" }
db.dbName.find() 

return cursor. 返回游标。 It is not array. 它不是数组。 You iterate through cursor two times (outer loop and inner loop), and this may be a problem. 您两次遍历游标(外循环和内循环),这可能是一个问题。 Check if this work 检查这项工作

input.forEach(function(word) {
 console.log(word);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM