简体   繁体   中英

MongoDB $filter doesn't work as expected

This is my projection stage of aggregation:

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)));

But it returnes elements from joins which their exited is set to true (and also false ).
Can you tell me what is my mistake?
(I should mention that list is an ArrayList of aggregation stages)

EDIT. This is one document I expect:

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

I want exited to be false all the time.

You have to parse the document value as it is getting interpreted as literal string value.

Update

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

to

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

or

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

Both variants should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM