MongoDB 个文档中的范围
Ranges in MongoDB documents
我想在 MongoDB 文档中定义范围,以便我可以通过属于特定范围的值进行查询。
例如,用 [min, max] 表示范围,如果我们考虑使用此表示法的三个文档的集合:
{
temperature: [-100, 10],
sensation: "Cold"
},
{
temperature: [10, 30],
sensation: "Mild"
},
{
temperature: [30, 50],
sensation: "Hot"
}
我想查询这些范围内的值并查看适合哪些文档:
temperature = 25.6 -> sensation = "Mild"
我知道我可以将最小值和最大值存储为单独的值,但也许有人可以用一种更优雅、更有效的方式来定义 MongoDB.
中的可索引范围
您需要使用索引为 min
的 dot notation 和数组中的 max
值。
db.collection.find(
{
"temperature.0": { "$lt": 25.6 },
"temperature.1": { "$gt": 25.6 }
},
{ "sensation": 1, "_id": 0 }
)
我想在 MongoDB 文档中定义范围,以便我可以通过属于特定范围的值进行查询。
例如,用 [min, max] 表示范围,如果我们考虑使用此表示法的三个文档的集合:
{
temperature: [-100, 10],
sensation: "Cold"
},
{
temperature: [10, 30],
sensation: "Mild"
},
{
temperature: [30, 50],
sensation: "Hot"
}
我想查询这些范围内的值并查看适合哪些文档:
temperature = 25.6 -> sensation = "Mild"
我知道我可以将最小值和最大值存储为单独的值,但也许有人可以用一种更优雅、更有效的方式来定义 MongoDB.
中的可索引范围您需要使用索引为 min
的 dot notation 和数组中的 max
值。
db.collection.find(
{
"temperature.0": { "$lt": 25.6 },
"temperature.1": { "$gt": 25.6 }
},
{ "sensation": 1, "_id": 0 }
)