【问题标题】:How to retrieve mongodb field value stored as array of string into a java ArrayList如何将存储为字符串数组的mongodb字段值检索到java ArrayList中
【发布时间】:2016-02-09 06:57:28
【问题描述】:

文档结构是:

db.lookupdata.insert({ parent_key : "category" , key :    "accessories" , value : ["belts","cases","gloves","hair","hats","scarves","sunglasses","ties","wallets","watches"]})

我想将数组字段值存储在 java 数组列表中 我正在寻找这样的文件:

 FindIterable<Document> iterable1 = docCollectionLookup.find(Filters.eq("parent_key", "category"));
        Iterator<Document> iter1=iterable1.iterator();
        while(iter1.hasNext())
        {
            Document theObj = iter1.next();

            categotyLookUpMap.put(theObj.getString("key"), list);

        }

现在我如何在 ArrayList 中检索数组字段值(键:“值”)

【问题讨论】:

    标签: mongodb-query mongodb-java


    【解决方案1】:

    您可以在 ArrayList 中检索数组字段值(键:“值”),就像检索字符串字段键一样。请参考:

    FindIterable<Document> iterable1 = docCollectionLookup.find(Filters.eq("parent_key", "category"));
    Iterator<Document> iter1=iterable1.iterator();
    
    //Create a HashMap variable with type <String,ArrayList>,according to your needs 
    Map<String,ArrayList> categotyLookUpMap = new HashMap<String,ArrayList>();
    
    while(iter1.hasNext())
    {
       Document theObj = iter1.next();
       //Get method of Document class will return object,parse it to ArrayList
       categotyLookUpMap.put(theObj.getString("key"), (ArrayList)theObj.get("value"));
    }
    

    或者,您可以使用Morphia,它是 Java 中的 MongoDB 对象-文档映射器。你可以从here设置依赖/下载JAR

    首先,创建 LookupData 类以映射到 lookupdata 集合。需要注释@Id,否则将抛出异常消息“没有用@Id 注释的字段;但它是必需的”。所以为它创建一个 _id 字段。

    @Entity("lookupdata")
    public class LookupData {
    
    @Id
    String _id ;
    
    @Property("parent_key")
    String parentKey;
    
    String key;
    
    ArrayList<String> value;
    
    public String get_id() {
        return _id;
    }
    public void set_id(String _id) {
        this._id = _id;
    }
    
    public String getParentKey() {
        return parentKey;
    }
    
    public void setParentKey(String parentKey) {
        this.parentKey = parentKey;
    }
    
    public String getKey() {
        return key;
    }
    
    public void setKey(String key) {
        this.key = key;
    }
    
    public void setValue(ArrayList<String> value) {
        this.value = value;
    }
    
    public ArrayList<String> getValue() {
        return value;
    }
    
    }
    

    检索数组字段值如下:

    MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost"));
    
    Morphia morphia = new Morphia();
    morphia.map(LookupData.class);           
    
    //lookupdata collection is under my local db "tutorials" in this case
    Datastore datastore = morphia.createDatastore(mongoClient, "tutorials");
    Map<String,ArrayList> categotyLookUpMap = new HashMap<String,ArrayList>();
    
    LookupData lookupData = datastore.find(LookupData.class).get();
    categotyLookUpMap.put(lookupData.getKey(), lookupData.getValue());
    

    【讨论】:

      猜你喜欢
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 2022-09-22
      • 1970-01-01
      相关资源
      最近更新 更多