【问题标题】:MongoDB $filter doesn't work as expectedMongoDB $filter 无法按预期工作
【发布时间】:2017-02-18 12:51:45
【问题描述】:

这是我的聚合投影阶段:

Document filter = new Document(
                            "$filter", new Document(
                            "input", "$joins").append("as", "join").append(
                            "cond", "{$eq: [\'$$join.exited\', false]}"));
list.add(project(new Document("_id", 0).append("joins", filter).append("userName", 1)
                                             .append("chatID", 1).append("warned", 1)));

但它会返回来自 joins 的元素,它们的 exited 设置为 true(以及 false)。
你能告诉我我的错误是什么吗?
(我应该提到list 是聚合阶段的ArrayList)

编辑。这是我期望的一份文件:

{
    userName: "test",
    //other fields than joins
    joins:
    [
        {
            remaining: 4
            userID: 1245
            exited: false
        },
        {
            remaining: 3
            userID: 2312
            exited: false
        }
    ]
}

我希望exited 一直是假的。

【问题讨论】:

  • 您可以添加您希望查询返回的示例文档吗?

标签: java mongodb aggregation-framework mongo-java mongo-java-driver


【解决方案1】:

您必须解析文档值,因为它被解释为文字字符串值。

更新

"cond", "{$eq: [\'$$join.exited\', false]}")

到

"cond", Document.parse("{$eq: [\'$$join.exited\', false]}")

或

"cond", new Document("$eq", Arrays.<Object>asList("$$join.exited", false))

这两种变体都应该可以工作。

【讨论】:

  • 工作就像一个魅力。谢谢
猜你喜欢
  • 1970-01-01
  • 2019-07-03
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 2013-10-06
  • 2021-11-28
  • 1970-01-01
  • 2013-12-23
相关资源
最近更新 更多