对于作业,我们会得到以下代码:
db.bib.insertMany( [
{type : "book",
"@year": "1994",
"title": "TCP/IP Illustrated",
"author": {
"last": "Stevens",
"first": "W."
},
"publisher": "Addison-Wesley",
"price": "65"
},
{type : "book",
"@year": "1992",
"title": "Unix Programming",
"author": {
"last": "Stevens",
"first": "W."
},
"publisher": "Addison-Wesley",
"price": "65"
},
{type : "book",
"@year": "2000",
"title": "Data on the Web",
"author": [
{
"last": "Abiteboul",
"first": "Serge"
},
{
"last": "Buneman",
"first": "Peter"
},
{
"last": "Suciu",
"first": "Dan"
}
],
"publisher": "Morgan Kaufmann",
"price": "39"
},
{type : "book",
"@year": "1999",
"title": "Digital TV",
"editor": {
"last": "Gerbarg",
"first": "Darcy",
"affiliation": "CITI"
},
"publisher": "Kluwer",
"price": "130"
}
,
{type : "journal",
"title": "Irreproducible results",
"editor": {
"last": "Self",
"first": "W."
},
"publisher": "SV"
}
])
使用mongoDB,我们会被要求完成不同的搜索查询,以便找到所需的信息。我目前坚持的那个是
列出1995年以后出版的书籍的标题,费用低于100.
根据我的理解,正确的查询应该是
db.bib.find({price: {$lt: 100}, year: {$gt: 1995}}, {title: 1, _id: 0})
但是,如果不这样做,则会提供空白结果。为什么这样,我该如何解决?
答案 0 :(得分:0)
两个字段的数据类型都是字符串。你无法将它们作为数字进行比较。尝试以下,它会工作。使用排序规则,您可以要求MongoDB将它们视为int。您可以阅读有关排序规则here at mongodb.com的更多信息。
db.bib.find({$and: [{"@year": {$gt: "1995"}}, {price: {$lt: "100"}}]}).collation({
locale: "en_US",
numericOrdering: true
});