作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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));
};
对于上面的代码,我试图找到每本书、期刊、音乐CD和杂志的数量。 IE 每个在我的文档中出现多少次。上面的代码能够以漂亮的格式返回整个文档。我如何找到每种类型的计数? (即图书数量、期刊数量、音乐CD数量和杂志数量)。请注意,我需要使用光标上的交互来完成任务!
示例输出为
number of books: 4
number of journals: 7
etc
blow是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"]
}
});
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"]
}
} }
]
}
});
最佳答案
您可以通过在光标
上进行一次迭代来完成此操作:如果考虑对字段(书籍、音乐CD等)进行计数,我知道这就是您正在寻找的内容。
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);
关于mongodb - 如何统计mongodb中游标的迭代次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58538198/
我是一名优秀的程序员,十分优秀!