【问题标题】:mongoTemplate query for composite keymongoTemplate 查询复合键
【发布时间】:2015-03-28 12:52:48
【问题描述】:

假设我有一个如下的 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 使用此查询的正确语法?

【问题讨论】:

    标签: mongodb spring-data mongotemplate


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多