如何在mongodb中的游标上进行迭代?

时间:2019-10-23 09:17:37

标签: mongodb iteration

我当前的任务要求

The queries listed below must be implemented by iterations over a cursor. 

我对游标的迭代方式感到困惑吗?香港专业教育学院试图自己研究以了解这一概念,但我仍然有点想法。

第一个问题是

 List the title and total number of keywords for each book. If a book has no
keywords, the total number of keywords must be 0 (zero). 

这是相关的JS脚本

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"]
    }
})

这是我的尝试

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));
};

此尝试打印,但不计数。

如果有人可以通过我的问题IVE解释这个概念,将不胜感激!

1 个答案:

答案 0 :(得分:0)

在游标上进行迭代意味着什么?

首先让我们看看什么是光标?

简单来说,光标是查询运行后的结果。

因此,执行查询后返回的数据/结果是游标。

read more about cursor

根据您提供的问题描述,这是我的理解:

您必须形成查询以完成任务,并遍历查询结果以输出结果。

您提供的脚本是将这些数据插入DB,然后对插入的数据执行操作/任务。

查询内容是什么?

我将那些留给你,

尝试...

如果需要帮助,

使用您尝试过的代码示例更新您的问题...

之后的评论:

这是解决方案

db.bookshop.aggregate({$match:{}},
    {$project:{"book.title":1, keywords: {$ifNull: ["$book.keywords", []]}}},
    {$project:{"book.title":1, noOfKeywords:{$size:"$keywords"}}})

我使用了 $ifNull operator ,来检查每个文档的书本对象中是否存在关键字数组

在此行

{$ifNull: ["$book.keywords", []]}

如果关键字数组为空或不存在,则它将用书对象中的[]替换特定文档的关键字字段。

然后在下一个 $project 阶段,我使用 $size 运算符计算数组的大小,这就是您的答案。 / p>