mongoDB javaAPi 添加嵌套数组数据 更新

import com.mongodb.*;
import com.mongodb.util.JSON;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import utils.MGUtil;

public class mongoApiText {
    public static void main(String[] args)throws Exception{
        Mongo mongo = MGUtil.getCon();
        DB db = mongo.getDB("mongo");
        DBCollection collection = db.getCollection("text");
        //添加数据。
        DBObject weiboName = new BasicDBObject();
        weiboName.put("_id", "123344");
        weiboName.put("type", "play");
        List area = new ArrayList();
        area.add("ssss2");
        area.add("ssssddd1");
        List weibos = new ArrayList();
        weibos.add(
                    BasicDBObjectBuilder.start().add("id", "1").add("text", "山水园").add("area",area).get());
        weibos.add(
                BasicDBObjectBuilder.start().add("id", "2").add("text", "滑雪").add("area",area).get());
        weiboName.put("weibos", weibos);
        collection.createIndex(new BasicDBObject("weibos",1));
        collection.insert(weiboName);

        //更新数据 一维
        DBObject weibos=new BasicDBObject();
        weibos.put("$set",new BasicDBObject("type","eat"));
        collection.update(new BasicDBObject(),weibos,false,true);
        
    }
}
        





你可能感兴趣的:(mongoDB javaAPi 添加嵌套数组数据 更新)