从嵌入式文档数组 Mongodb Java 中获取字段的键

这是我的 MongoDB 文档的简化模式:


{

    "_id": 0,

    "config": [{

            "property1": "a",

            "property2": "b",

            "property3": "c",

            "property4": "d"

        },

        {

            "property1": "a",

            "property2": "bb",

            "property3": "cc",

            "property4": "d",

            "ispropert5": true

        },

        {

            "property1": "a",

            "property2": "b",

            "property3": "c",

            "property4": "ddd",

            "ispropert5": false,

            "ispropert6": false

        }

    ],

    "entity": "123asdf",

    "url": "",

    "createdDate": 1

}

作为输出,我需要获取嵌套文档的唯一键列表:{property1, property2, property3, property4, ispropert5, ispropert6}


我正在我的课堂上尝试这个,但当然无法将 ArrayList 转换为 Document:


 Document dbo = col.find().first();

        Set<String> keys = dbo.keySet();

        Iterator iterator = keys.iterator();


        while(iterator.hasNext()) {

            String key = iterator.next().toString();

            if(dbo.get(key) instanceof ArrayList){

                Document dboNested = (Document) dbo.get(key); //java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.bson.Document

                Set<String> keysNested = dboNested.keySet();

                System.out.println("KeyNested: " + keysNested);

            }

        }


叮当猫咪
浏览 205回答 3
3回答

小唯快跑啊

就像您评论的那样,在您的代码中,您无法将 ArrayList 转换为 Document,因此您必须将其转换为如下所示的 ArrayList:&nbsp;while(iterator.hasNext()) {&nbsp; &nbsp; String key = iterator.next().toString();&nbsp; &nbsp; Object value = dbo.get(key);&nbsp; &nbsp; if (value instanceof ArrayList){&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<?> dboArrayNested = (ArrayList<?>) value;&nbsp; &nbsp; &nbsp; &nbsp; for (Object dboNestedObj : dboArrayNested) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dboNestedObj instanceof Document) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printKeysNested(Document.class.cast(dboNestedObj));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; // else if extra...&nbsp; &nbsp; } else if (value instanceof Document) {&nbsp; &nbsp; &nbsp; &nbsp; printKeysNested(Document.class.cast(value));&nbsp; &nbsp; }}public static void printKeysNested(Document dboNested) {&nbsp; &nbsp; Set<String> keysNested = dboNested.keySet();&nbsp; &nbsp; System.out.println("KeyNested: " + keysNested);}我希望这对你有帮助。

江户川乱折腾

感谢所有的答案,这是我的最终代码:while (iterator.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String key = iterator.next().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object value = dbo.get(key);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (value instanceof ArrayList) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<?> dboArrayNested = (ArrayList<?>) value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Object dboNestedObj : dboArrayNested) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Document dboNested = Document.class.cast(dboNestedObj);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dboNestedObj instanceof Document) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keysNested.addAll(dboNested.keySet());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // else if extra...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (value instanceof Document) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Document dboNested = Document.class.cast(value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; keysNested.addAll(dboNested.keySet());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("KeysNested: " + keysNested);给定示例的输出:[property1, property2, property3, property4, ispropert5, ispropert6]

饮歌长啸

我想我来晚了,但我正在尝试一种更 mongo 的方式来做到这一点。您肯定希望在一小部分查询上执行此操作,match甚至可能是开始此操作之前的一个阶段db.coll.aggregate([{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $unwind: '$config'&nbsp; &nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $project: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $objectToArray: '$config'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $unwind: '$arr'&nbsp; &nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $group: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _id: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key: '$arr.k'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $project: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key: '$_id.key',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _id: 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java