1

假设我有一个 mongo 集合,如下所示:

/* 0 */
{
    "_id" : {
        "index" : "index1",
        "version" : 1
        }
}

/* 1 */
{
    "_id" : {
        "index" : "index2",
        "version" : 2
    }
}

/* 2 */
{
    "_id" : {
        "index" : "index1",
        "version" : 3
    }
}

我想使用 Spring 的 mongoTemplate 编写一个查询来仅检索那些具有 _id.index = index1 的文档。

使用 mongo shell 我可以编写如下查询:

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 帮助我使用此查询的正确语法吗?

4

1 回答 1

1

抱歉,这不是一个真正有效的问题。我在问题中引用的 mongoTemplate 查询确实有效。我用错误的 _id.index 调用它,哎呀 :)

我似乎无法删除该问题,但也许它会帮助某人进行复合键查询...

于 2015-01-29T10:47:18.457 回答