Mongodb 复合密钥的mongoTemplate查询
假设我有一个mongo系列,如下所示:Mongodb 复合密钥的mongoTemplate查询,mongodb,spring-data,mongotemplate,Mongodb,Spring Data,Mongotemplate,假设我有一个mongo系列,如下所示: /* 0 */ { "_id" : { "index" : "index1", "version" : 1 } } /* 1 */ { "_id" : { "index" : "index2", "version" : 2 } } /* 2 */ { "_id" : { "index" : "index1",
/* 0 */
{
"_id" : {
"index" : "index1",
"version" : 1
}
}
/* 1 */
{
"_id" : {
"index" : "index2",
"version" : 2
}
}
/* 2 */
{
"_id" : {
"index" : "index1",
"version" : 3
}
}
db.collectionName.find({"_id.index" : "index1"})
我想使用Spring的mongoTemplate编写一个查询,只检索那些_id.index=index1的文档
使用mongo shell,我可以编写以下查询:
/* 0 */
{
"_id" : {
"index" : "index1",
"version" : 1
}
}
/* 1 */
{
"_id" : {
"index" : "index2",
"version" : 2
}
}
/* 2 */
{
"_id" : {
"index" : "index1",
"version" : 3
}
}
db.collectionName.find({"_id.index" : "index1"})
然而,我认为使用mongoTemplate会起作用的东西却不起作用。我试过:
Query query = new Query();
query.addCriteria(Criteria.where("_id.index").is("index1"));
mongoTemplate.find(query, SomeJavaObject.class, COLLECTION_NAME);
有谁能使用mongoTemplate帮助我了解此查询的正确语法吗?抱歉,这不是一个真正有效的问题。我在问题中引用的mongoTemplate查询确实有效。我用错误的_id.index调用它,哎呀:) 我似乎无法删除该问题,但它可能会帮助某人执行复合键查询