簡體   English   中英

如何在 mongodb 中迭代 cursor?

[英]how to iterate over a cursor in mongodb?

我目前的任務要求

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

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

此嘗試打印但不算數。

如果有人可以通過我提供的問題來解釋這個概念,那將不勝感激!

cursor 上的迭代意味着什么?

讓我們先看看cursor 是什么?

簡單來說, cursor是查詢運行后的結果。

所以執行查詢后返回的數據/結果是 cursor。

閱讀更多關於 cursor

根據您提供的問題描述,這是我的理解:

您必須形成一個查詢來完成您的任務並將查詢結果迭代到 output 結果。

您提供的腳本是將這些數據插入數據庫,然后對這些插入的數據執行您的操作/任務。

查詢會是什么?

我會把這些留給你,

試試看...

如果你需要幫助,

用您嘗試過的代碼示例更新您的問題...

評論后:

這是解決方案

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

我使用$ifNull運算符來檢查每個文檔的 object 書中是否存在關鍵字數組,

在這一行

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

如果關鍵字數組是 null 或不存在,那么它將用 object 書中的[]替換該特定文檔的關鍵字字段。

然后在下一個$project stage ,我使用$size運算符來計算數組的大小,這就是你的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM