首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MongoDB Java错误:管道阶段规范对象必须恰好包含一个字段

MongoDB Java错误:管道阶段规范对象必须恰好包含一个字段
EN

Stack Overflow用户
提问于 2021-03-30 02:09:15
回答 1查看 231关注 0票数 0

我正在尝试在java上获取这个MongoDB查询的结果。

代码语言:javascript
运行
复制
db.fileTree.aggregate([
    {
        $match: {
            "_id": "6062144bb25e4809548ef246",
        }
    }, 
    {
        $unwind: "$children"
    },
    {
        $match: {
            "children.fileName": "Test1"
        }
    },
    {
        $project: {
            "_id": 0,
            "fileId": "$children.fileId",
            "fileName": "$children.fileName",
            "directory": "$children.directory",
        }
    }
]).pretty()

查询运行得非常好,当没有数据时,它不会显示任何内容。但是,当从java执行查询时,会产生以下错误:

com.mongodb.MongoCommandException: Command failed with error 40323 (Location40323): 'A pipeline stage specification object must contain exactly one field.' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "A pipeline stage specification object must contain exactly one field.", "code": 40323, "codeName": "Location40323"}

代码语言:javascript
运行
复制
ChildFile findChildInParent(String parentId, String fileName) {
        BasicDBObject idFilter = new BasicDBObject().append("_id", parentId);
        BasicDBObject matchId = new BasicDBObject().append("$match", idFilter);
        BasicDBObject unwindChildren = new BasicDBObject().append("$unwind", "$children");
        BasicDBObject childNameFilter = new BasicDBObject().append("children.fileName", fileName);
        BasicDBObject matchChildName = new BasicDBObject().append("$match", childNameFilter);
        BasicDBObject projections = new BasicDBObject()
                .append("_id", 0)
                .append("fileId", "$children.fileId")
                .append("fileName", "$children.fileName")
                .append("directory", "$children.directory");

        List<ChildFile> childFiles = fileCollection.aggregate(
                List.of(matchId, unwindChildren, matchChildName, projections),
                ChildFile.class
        ).into(new ArrayList<>());

        return childFiles.size() > 0 ? childFiles.get(0) : null;
    }

我是不是漏掉了什么?任何帮助都是非常感谢的。谢谢你,?!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-30 02:25:11

您的代码中有一个拼写错误,缺少children字段的$

应该是:

代码语言:javascript
运行
复制
BasicDBObject unwindChildren = new BasicDBObject().append("$unwind", "$children")

而不是:

代码语言:javascript
运行
复制
BasicDBObject unwindChildren = new BasicDBObject().append("$unwind", "children")

还缺少$poject阶段:

代码语言:javascript
运行
复制
BasicDBObject projections = new BasicDBObject()
        .append("_id", 0)
        .append("fileId", "$children.fileId")
        .append("fileName", "$children.fileName")
        .append("directory", "$children.directory");

BasicDBObject projectionStage = new BasicDBObject().append("$project", projections);

List<ChildFile> childFiles = fileCollection.aggregate(
        List.of(matchId, unwindChildren, matchChildName, projectionStage),
        ChildFile.class
).into(new ArrayList<>());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66859375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档