简体   繁体   中英

In Java MongoDb how can I save the values of a specific field in an ArrayList?

Suppose we have the following documents:

  {{i:1},{i:9},{i:10}}
  {i:3}
  {{i:4},{i:0}}
  {{i:5},{i:-3},{i:30}}

each line represents a document

is it possible to save the values of i in an ArrayList or some kind of List in general?

I'm trying to achieve this in java

thanks in advance

If you alter your documents slightly as follows:

{ivalues:[{i:1},{i:9},{i:10}]}

Then you should be able to create the document in Java as follows:

ArrayList<DBObject> x = new ArrayList<DBObject>();
x.add(new BasicDBObject("i", 1));
x.add(new BasicDBObject("i", 9));
x.add(new BasicDBObject("i", 10));
BasicDBObject doc = new BasicDBObject("ivalues", x);

And save/retrieve it as normal.

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