这是我应该检查条件的json
[
{
"name" : "Rice",
"mintemp" : "22",
"maxtemp" : "32",
"minrainfall" : "150",
"maxrainfall" : "300",
"soil" : ""
},
{
"name" : "Wheat",
"mintemp" : "10",
"maxtemp" : "16",
"minrainfall" : "75",
"maxrainfall" : "100",
"soil" : ""
},
{
"name" : "Millets",
"mintemp" : "27",
"maxtemp" : "32",
"minrainfall" : "50",
"maxrainfall" : "100",
"soil" : ""
},
{
"name" : "Grams",
"mintemp" : "20",
"maxtemp" : "25",
"minrainfall" : "40",
"maxrainfall" : "45",
"soil" : ""
},
{
"name" : "Sugar Cane",
"mintemp" : "21",
"maxtemp" : "27",
"minrainfall" : "75",
"maxrainfall" : "150",
"soil" : ""
},
{
"name" : "Cotton",
"mintemp" : "21",
"maxtemp" : "30",
"minrainfall" : "50",
"maxrainfall" : "75",
"soil" : ""
},
{
"name" : "Coffee",
"mintemp" : "15",
"maxtemp" : "28",
"minrainfall" : "150",
"maxrainfall" : "250",
"soil" : ""
}
]
编写查询以显示所有名称,其中mintemp应该在16到20之间,而maxtemp也应该在16到20之间
预期输出:
[
{
"name" : "Wheat",
"mintemp" : "10",
"maxtemp" : "16",
"minrainfall" : "75",
"maxrainfall" : "100",
"soil" : ""
},
{
"name" : "Grams",
"mintemp" : "20",
"maxtemp" : "25",
"minrainfall" : "40",
"maxrainfall" : "45",
"soil" : ""
},
{
"name" : "Coffee",
"mintemp" : "15",
"maxtemp" : "28",
"minrainfall" : "150",
"maxrainfall" : "250",
"soil" : ""
}
]
通过mongoplayground.net进行检查并发送链接
https://mongoplayground.net/p/jV0e00_FQ_P
上面的链接包含json,仅键入查询
答案 0 :(得分:5)
欢迎来到StackOverlow!
请注意,JSON格式确实知道integer
数据类型。使用以下JSON数据:
[
{
"name": "Rice",
"mintemp": 22,
"maxtemp": 32,
"minrainfall": 150,
"maxrainfall": 300,
"soil": ""
},
{
"name": "Wheat",
"mintemp": 10,
"maxtemp": 16,
"minrainfall": 75,
"maxrainfall": 100,
"soil": ""
},
{
"name": "Millets",
"mintemp": 27,
"maxtemp": 32,
"minrainfall": 50,
"maxrainfall": 100,
"soil": ""
},
{
"name": "Grams",
"mintemp": 20,
"maxtemp": 25,
"minrainfall": 40,
"maxrainfall": 45,
"soil": ""
},
{
"name": "Sugar Cane",
"mintemp": 21,
"maxtemp": 27,
"minrainfall": 75,
"maxrainfall": 150,
"soil": ""
},
{
"name": "Cotton",
"mintemp": 21,
"maxtemp": 30,
"minrainfall": 50,
"maxrainfall": 75,
"soil": ""
},
{
"name": "Coffee",
"mintemp": 15,
"maxtemp": 28,
"minrainfall": 150,
"maxrainfall": 250,
"soil": ""
}
]
从这里开始,您可以使用以下解决方案来查询数据以获取范围:
db.collection.find({
$or: [
{
mintemp: {
$gte: 16,
$lte: 20
}
},
{
maxtemp: {
$gte: 16,
$lte: 20
}
}
]
})
请参阅我的solution proposal。