【问题标题】:Find in mongodb subdocument在 mongodb 子文档中查找
【发布时间】:2017-07-07 16:51:16
【问题描述】:

我使用 SpringData。我有带有子文档的文档,它看起来像:

{ “名称”:“MongoDB”, “类型”:“数据库”, “计数”:1, “信息”:{ ×:203, 是:102 } }

我怎样才能找到(例如)x=203 的所有文档 谢谢!

【问题讨论】:

  • 我想检索所有 x=203 的文档
  • 我问你有什么尝试。不是你想要的。
  • 类似这样的代码 mongoTemplate.find(new Query(Criteria.where("info").elemMatch(Criteria.where("x").is(203))), InfoDocument.class) );
  • 您不能使用$elemMatch 来匹配文档的一部分。您可以使用 . 运算符直接匹配子元素。或许你可以试试:-mongoTemplate.find(new Query(Criteria.where("info.x").is(203))), InfoDocument.class));
  • 之前查过,不行

标签: java mongodb nosql


【解决方案1】:

您无法直接获取子文档。您可以做的是使用下面的查询来匹配子文档中的值。如果子文档的标准成功,这将检索父文档。正如 Rohit 提到的,您可以使用下面的查询,但这会返回您的父文档的类型

mongoTemplate.find(new Query(Criteria.where("info.x").is(203))), ParentDocument.class));

【讨论】:

    猜你喜欢
    • 2021-07-25
    • 2016-08-25
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 2021-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多