Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB,Java-从JSON查询到文档_Java_Json_Mongodb_Aggregation Framework_Mongodb Java - Fatal编程技术网

MongoDB,Java-从JSON查询到文档

MongoDB,Java-从JSON查询到文档,java,json,mongodb,aggregation-framework,mongodb-java,Java,Json,Mongodb,Aggregation Framework,Mongodb Java,Hy 我需要一些帮助来将以下MongoDB查询转换为MongoDB Java驱动程序查询 请注意,查询是有效的 db.days.aggregate([ { $match: { 'day' : 'March_1'}}, { $project: { _id : 0, day: 1, events: {$filter: { input: '$events', as: 'event',

Hy

我需要一些帮助来将以下MongoDB查询转换为MongoDB Java驱动程序查询

请注意,查询是有效的

db.days.aggregate([
    { $match: { 'day' : 'March_1'}},
    { $project: {
        _id : 0,
        day: 1,
        events: {$filter: {
            input: '$events',
            as: 'event',
            cond: {$eq: ['$$event.year', '2002']}
        }}
    }}
])
我的尝试是这样的,但是失败了,我需要你的帮助

Document query = new Document("$match", new Document("day", day)).
    append("$project", new Document("_id", 0).
            append("day", 1).
            append("events", new Document("$filter", new Document(
                    "input", "$" + category).
                    append("as", "event").
                    append("cond", new Document("$eq", Arrays.asList("$$event.year", year))))));
我得到的错误是

"{ "ok" : 0.0, "errmsg" : "A pipeline stage specification object must contain exactly one field.", "code" : 16435 }"

多谢各位

不要将$match和$project放在同一个对象中,请使用列表

AggregateIterable<Document> iterable = collection.aggregate(
  asList(
    new Document("$match", new Document("day", day)),
    new Document("$project", 
        new Document("_id", "0")
            .append("day", 1)
            .append(
                "events", 
                new Document(
                    "$filter",
                    new Document("input", "$events")
                        .append("as", "event")
                        .append(
                            "cond", 
                            new Document("eq", Arrays.asList("$$event.year", year))
                        )
                )
            )
    )
   )
)   
AggregateIterable=collection.aggregate(
asList(
新文档(“$match”,新文档(“day”,day)),
新文档(“$project”,
新文档(“\u id”,“0”)
.append(“day”,1)
.附加(
“活动”,
新文件(
“$filter”,
新文档(“输入”,“$events”)
.append(“as”,“event”)
.附加(
“康德”,
新文档(“eq”,Arrays.asList($$event.year,year))
)
)
)
)
)
)   

不要将$match和$project放在同一个对象中,请使用列表

AggregateIterable<Document> iterable = collection.aggregate(
  asList(
    new Document("$match", new Document("day", day)),
    new Document("$project", 
        new Document("_id", "0")
            .append("day", 1)
            .append(
                "events", 
                new Document(
                    "$filter",
                    new Document("input", "$events")
                        .append("as", "event")
                        .append(
                            "cond", 
                            new Document("eq", Arrays.asList("$$event.year", year))
                        )
                )
            )
    )
   )
)   
AggregateIterable=collection.aggregate(
asList(
新文档(“$match”,新文档(“day”,day)),
新文档(“$project”,
新文档(“\u id”,“0”)
.append(“day”,1)
.附加(
“活动”,
新文件(
“$filter”,
新文档(“输入”,“$events”)
.append(“as”,“event”)
.附加(
“康德”,
新文档(“eq”,Arrays.asList($$event.year,year))
)
)
)
)
)
)   

您遇到了什么错误?请参阅编辑。非常感谢。我认为问题在于我构建了一个文档而不是数组。但我找不到任何使用文档构建数组的内容。您遇到了什么错误?请参阅编辑。非常感谢。我认为问题在于我构建了一个文档而不是数组。但是我找不到任何使用文档构建数组的东西。非常感谢!几天前我刚开始使用MongoDB,我将用Java自杀……非常感谢!几天前我刚开始使用MongoDB,我打算用Java自杀。。。