package mongoUtil; import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** */ public class MongodbutilTest { public static void main(String args[]){ MongodbutilTest a= new MongodbutilTest(); // a.queryByUid(); a.addDocs(); //a.queryByTagsIdAndValue(); //a.queryByUidAndUpdateTagValue(); // a.queryByUidAndDeleteTag(); // a.queryByUidAndAddTag(); //a.addIndex(); } public void addDocs(){ MongoHelper a = new MongoHelper(); MongoDatabase c = a.getMongoDataBase(); MongoCollection<Document> collection= c.getCollection("bdp_user_tag_test2"); for(int index = 0;index < 1000 ;index++){ //BASE_INFO_LIST List baseInfoList = new ArrayList<Document>(); Document baseInfo1 = new Document("INFO_CODE", "BASE01").append("INFO_VALUE","1"); Document baseInfo2 = new Document("INFO_CODE", "BASE02").append("INFO_VALUE","2"); Document baseInfo3 = new Document("INFO_CODE", "BASE0"+index).append("INFO_VALUE","0"+index); baseInfoList.add(baseInfo1); baseInfoList.add(baseInfo2); baseInfoList.add(baseInfo3); List baseList = new ArrayList<Document>(); Document basetag1 = new Document("TAG_CODE","1").append("TAG_VALUE","01"); Document basetag2 = new Document("TAG_CODE","2").append("TAG_VALUE","02"); Document basetag3 = new Document("TAG_CODE",""+index).append("TAG_VALUE","0"+index); baseList.add(basetag1); baseList.add(basetag2); baseList.add(basetag3); List etOneList = new ArrayList<Document>(); Document ettag1 = new Document("ET_CODE","ET01").append("ET_VALUE","AAAAA"); Document ettag2 = new Document("ET_CODE","ET02").append("ET_VALUE","BBBBB"); Document ettag3 = new Document("ET_CODE","ET0"+index).append("ET_VALUE","CCCCC"+index); etOneList.add(ettag1); etOneList.add(ettag2); etOneList.add(ettag3); //List etList2 = new ArrayList<List>(); List etOne2List = new ArrayList<Document>(); Document ettag21 = new Document("ET_CODE","ET01").append("ET_VALUE","AAAAA"); Document ettag22 = new Document("ET_CODE","ET02").append("ET_VALUE","BBBBB"); Document ettag23 = new Document("ET_CODE","ET03").append("ET_VALUE","CCCCC"); etOne2List.add(ettag21); etOne2List.add(ettag22); etOne2List.add(ettag23); List document2 = new ArrayList<Document>(); Document documentEle1 = new Document("ET_ONE_LIST",etOneList); Document documentEle2 = new Document("ET_ONE_LIST",etOne2List); document2.add(documentEle1); document2.add(documentEle2); Document document = new Document("UID","00000000"+index) .append("CREATE_DATE","2016-10-21 00:00:00") .append("OP_DATE","2016-10-21 00:00:00") .append("BASE_INFO_LIST",baseInfoList) .append("BASE_TAG_LIST",baseList) .append("ET_TAG_LIST",document2); System.out.println(index); collection.insertOne(document); } } }
数据格式:
{
"_id" : ObjectId("5bc058e679204c04a49e67be"),
"UID" : "000001000",
"CREATE_DATE" : "2016-10-21 00:00:00",
"OP_DATE" : "2016-10-21 00:00:00",
"BASE_INFO_LIST" : [
{
"INFO_CODE" : "BASE01",
"INFO_VALUE" : "1"
},
{
"INFO_CODE" : "BASE02",
"INFO_VALUE" : "2"
},
{
"INFO_CODE" : "BASE01",
"INFO_VALUE" : "3"
}
],
"BASE_TAG_LIST" : [
{
"TAG_CODE" : "1",
"TAG_VALUE" : "01"
},
{
"TAG_CODE" : "2",
"TAG_VALUE" : "02"
},
{
"TAG_CODE" : "3",
"TAG_VALUE" : "03"
}
],
"ET_TAG_LIST" : [
{
"ET_ONE_LIST" : [
{
"ET_CODE" : "ET01",
"ET_VALUE" : "AAAAA"
},
{
"ET_CODE" : "ET02",
"ET_VALUE" : "BBBBB"
},
{
"ET_CODE" : "ET03",
"ET_VALUE" : "CCCCC"
}
]
},
{
"ET_ONE_LIST" : [
{
"ET_CODE" : "ET01",
"ET_VALUE" : "AAAAA"
},
{
"ET_CODE" : "ET02",
"ET_VALUE" : "BBBBB"
},
{
"ET_CODE" : "ET03",
"ET_VALUE" : "CCCCC"
}
]
}
]
}