【发布时间】:2016-08-25 08:34:15
【问题描述】:
由于 mongodb 中的每个集合在 _id 列上都有一个默认索引,我想在我的场景中利用它,如下所示。
我的收藏如下,
{
"_id":{
"timestamp" : ISODate("2016-08-24T23:22:20.201Z"),
"departmentname" : "sales",
"city":"NJ"
}
//Other fields in my collection
}
有了这个结构,我可以查询如下,
db.getCollection('test').find(
{
"_id" : {
"timestamp" : ISODate("2016-08-21T23:22:20.201Z"),
"departmentname" : "sales",
"city":"NJ"
}
}
)
但是,当我通过以下 _id 列的一个或多个字段进行查询时,
db.getCollection('test').find(
{
"_id" : {
"timestamp" : ISODate("2016-08-21T23:22:20.201Z")
}
}
)
(或)
db.getCollection('test').find(
{
"_id" : {
"departmentname" : "sales"
}
}
)
(或)
db.getCollection('test').find(
{
"_id" : {
"departmentname" : "sales",
"city":"NJ"
}
}
)
我没有看到任何返回的文件
当我使用 .explain() 检查时,我发现它使用了索引,但没有找到任何文档。
另外,我想对时间戳字段进行日期范围查询,同时查询 _id 列中的一个或多个字段,如下所示,
db.getCollection('test').find(
{
"_id.timestamp" : {
"$gte": ISODate("2011-08-21T23:22:20.201Z")
},
"_id.departmentname" : "sales"
}
)
但是,我没有看到任何返回的文件。当我运行 .explain() 时,我看到它使用了 colscan 而不是索引。
有人可以帮助我以正确的方式通过我的 _id 列上的一个或多个字段进行查询。
谢谢,
斯里
【问题讨论】:
标签: mongodb spring-data-mongodb bson