首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在mongodb中如何计算游标迭代次数?

在mongodb中如何计算游标迭代次数?
EN

Stack Overflow用户
提问于 2019-10-24 17:23:27
回答 1查看 69关注 0票数 2
代码语言:javascript
复制
var myCursor = db.bookshop.aggregate([]).pretty();

while (myCursor.hasNext()) {
   var x = myCursor.next();
   if(x.book)
   print(tojson(x));
};

while (myCursor.hasNext()) {
   var x = myCursor.next();
   if(x.journal)
   print(tojson(x));
};

while (myCursor.hasNext()) {
   var x = myCursor.next();
   if(x.musicCD)
   print(tojson(x));
};

while (myCursor.hasNext()) {
   var x = myCursor.next();
   if(x.magazine)
   print(tojson(x));
};

对于上面的代码,我尝试查找每本书、期刊、musicCD和杂志的数量。即每一个在我的文档中出现的次数。上面的代码能够以一种漂亮的格式返回整个文档。我如何找到每种类型的计数?(即图书数量、期刊数量、musicCD数量和杂志数量)。注意:我需要使用光标上的迭代来完成任务!

输出示例如下

代码语言:javascript
复制
number of books: 4
number of journals: 7
etc

blow是js文件(它很大),任何想要参考它的人都可以使用它。

代码语言:javascript
复制
 db.bookshop.insert( {
    "_id":"185.3.16",
    "book": {
        "callnum":"185.3.16",
        "isbn":"1-292-06118-9",
        "title":"Database Systems",
        "authors":[
            {
                "fname":"Thomas",
                "lname":"Connolly"},
            { 
                "fname":"Carolyn",
                "lname":"Begg"}
        ],
        "publisher":"Pearson Pty Ltd",
        "year":2015,
        "price":136.99,
        "topic":"Computer Science",
        "description":"This is the 6th edition. You can register online to access the examples",
        "keywords":["Database", "XML", "Distributed"]
        }
});

db.bookshop.insert( {
    "_id":"163.24.12",
    "book": {
        "callnum":"163.24.12",
        "isbn":"1-123-456-810",
        "title":"Core Java",
        "authors":[
            {
                "fname":"Horstmann",
                "lname":"Cornell"}
        ],
        "publisher":"PH Pty Ltd",
        "year":2012,
        "price":142.90,
        "topic":"Computer Science",
        "description":"It covers JAVA programming and JAVA script",
        "keywords":["JAVA", "XML", "Script"]
    }
});

db.bookshop.insert( {
    "_id":"123.45.67",
    "book": {
        "callnum":"123.45.67",
        "isbn":"1-123-456-789",
        "title":"Algorithms",
        "authors":[
            {
                "fname":"James",
                "lname":"Bond"},
            {
                "fname":"Harry",
                "lname":"Potter"},
            {
                "fname":"William",
                "lname":"Stallings"}
        ],
        "publisher":"Pearson Pty Ltd",
        "year":2013,
        "price":65.85,
        "topic":"Computer Science",
        "description":"It contains algorithms and their applications. You can download examples from the website"
    }
});
db.bookshop.insert( {
    "_id":"134.41.33",
    "book": {
        "callnum":"134.41.33",
        "isbn":"1-213-431-770",
        "title":"C++ Programming",
        "authors":[
            {
                "fname":"Larry",
                "lname":"Peterson"}
        ],
        "publisher":"Pearson Pty Ltd",
        "year":2010,
        "price":74.90,
        "topic":"Computer Science",
        "description":"C++ programming and its applications",
        "keywords":["C++", "Class", "Overloading", "Inheritance"]
    }
});

db.bookshop.insert( {
    "_id":"214.56.82 12.23",
    "journal": {
        "callnum":"214.56.82",
        "issn":"S-11-123-123-456",
        "title":"Handyman",
        "vol":12,
        "issue":23,
        "price":9.85,
        "year":2017,
        "month":"February"
    }
});

db.bookshop.insert( {
    "_id":"214.56.82 12.24",
    "journal": {
        "callnum":"214.56.82",
        "issn":"S-11-123-123-456",
        "title":"Handyman",
        "vol":12,
        "issue":24,
        "price":9.85,
        "year":2017,
        "month":"August"
    }
});


db.bookshop.insert( {
    "_id":"214.56.82 12.25",
    "journal": {
        "callnum":"214.56.82",
        "issn":"S-11-123-123-456",
        "title":"Handyman",
        "vol":12,
        "issue":25,
        "price":9.85,
        "year":2017,
        "month":"October"
    }
});

db.bookshop.insert( {
    "_id":"263.18.37 5.33",
    "journal": {
        "callnum":"263.18.37",
        "issn":"D-10-123-124-456",
        "title":"Information processing",
        "vol":5,
        "issue":33,
        "price":15.90,
        "year":2018,
        "month":"January"
    }
});

db.bookshop.insert( {
    "_id":"263.18.37 5.34",
    "journal": {
        "callnum":"263.18.37",
        "issn":"D-10-123-124-456",
        "title":"Information processing",
        "vol":5,
        "issue":34,
        "price":15.90,
        "year":2018,
        "month":"May"
    }
});

db.bookshop.insert( {
    "_id":"285.32.74 17.65",
    "journal": {
        "callnum":"285.32.74",
        "issn":"C-9-123-456-666",
        "title":"Mathmetics and Computing",
        "vol":17,
        "issue":65,
        "price":35.90,
        "year":2018,
        "month":"December"
    }
});

db.bookshop.insert( {
    "_id":"285.32.74 17.66",
    "journal": {
        "callnum":"285.32.74",
        "issn":"C-9-123-456-666",
        "title":"Mathmetics and Computing",
        "vol":17,
        "issue":66,
        "price":35.90,
        "year":2019,
        "month":"February"
    }
});

db.bookshop.insert( {
    "_id":"321.12.76",
    "musicCD": {
        "callnum":"321.12.76",
        "title":"Music",
        "producer":"ABC Pty Ltd",
        "year":2010,
        "category":"Pop",
        "price":12.80
    }
});

db.bookshop.insert( {
    "_id":"342.22.42",
    "musicCD": {
        "callnum":"342.22.42",
        "title":"Stars",
        "producer":"BBC Pty Ltd",
        "year":2012,
        "category":"Classic",
        "price":12.80
    }
});

db.bookshop.insert( {
    "_id":"332.17.25",
    "musicCD": {
        "callnum":"332.17.25",
        "title":"Wiggle Wiggle",
        "producer":"Wiggle Pty Ltd",
        "year":2005,
        "category":"Kids",
        "price":10.50
    }
});

db.bookshop.insert( {
    "_id":"402.12.1",
    "magazine": {
        "callnum":"402.12.1",
        "title":"ORACLE",
        "publisher":"Julia McVeigh",
        "topic":"Database",
        "price":4.50,
        "year":2017,
        "month":"June",
        "contents":[
            {"event":[{"current":{"page":14}}, {"upcoming":{"page":15}}]},
            {"columns":[
                {"title":"Getting good service", "page":17, "author":"Jeff Spicer"},
                {"title":"Behind Unbreakable", "page":19, "author":"Ken Jacobs"},
                {"title":"Getting good", "page":21, "author":"Rich Niemiec"},
                {"title":"Building and using portals", "page":25, "author":"Robert Hall"},
                {"title":"Blocks, messages, rows", "page":29, "author":"Tom Kyte"}
            ]},
            {"upfront":{
                "Oracle monitor":{
                    "page":39,
                    "titles":["JDeveloper arrives", "Oracle and network", "Supply chain goes with flow"]
                }, 
                "Did you know":{"page":40}, 
                "Share pool":{
                    "page":53,
                    "titles":["Systems expands content-management", "ArtinSoft automates migration to Java"]
                }
            } }
        ]
    }
});

db.bookshop.insert( {
    "_id":"402.12.2",
    "magazine": {
        "callnum":"402.12.2",
        "title":"ORACLE",
        "publisher":"Julia McVeigh",
        "topic":"Database",
        "price":4.50,
        "year":2019,
        "month":"July",
        "contents":[
            {"event":[{"current":{"page":9}}, {"upcoming":{"page":10}}]},
            {"columns":[
                {"title":"Making integration", "page":15, "author":"Jeff Spicer"},
                {"title":"Oracle retrospective", "page":21, "author":"Rich Niemiec"},
                {"title":"Stretching the Web", "page":27, "author":"Robert Hall"},
                {"title":"Beyond Init.ora and SYS", "page":29, "author":"Tom Kyte"}
            ]},
            {"upfront":{
                "Oracle monitor":{
                    "page":39,
                    "titles":["Oracle information architecture", "Certifications debut at OpenWorld", "Big new for small business", "V-business on the horizon"]
                }, 
                "Did you know":{"page":40}, 
                "Share pool":{
                    "page":47,
                    "titles":["Luminate. Net intelligent database management", "Unicenter supports Oracle", "SDA's Web database objects"]
                }
            } }
        ]
    }
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-24 20:27:48

您可以在cursor上进行一次迭代:如果字段(book、musicCD等)被认为是为了计数,我知道这是你要找的。

代码语言:javascript
复制
var cursor = db.bookshop.find(); //find will give the cursor as well
var bookCount = 0;
var journalCount = 0;
var musicCDCount = 0;
var magazineCount = 0;
while (cursor.hasNext()) {
  var current = cursor.next();
  if (typeof current["book"] !== "undefined") {
    bookCount++;
  }

  if (typeof current["journal"] !== "undefined") {
    journalCount++;
  }

  if (typeof current["musicCD"] !== "undefined") {
    musicCDCount++;
  }

  if (typeof current["magazine"] !== "undefined") {
    magazineCount++;
  }
}
print("number of books: " + bookCount);
print("number of journals: " + journalCount);
print("number of musicCDs: " + musicCDCount);
print("number of magazines: " + magazineCount);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58538198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档