技术开发 频道

开篇文章:NoSQL之MongoDB基本操作

  5、数据插入

  批量插入:

List datas = new ArrayList();
for (int i=0; i < 100; i++) {
BasicDBObject bo
= new BasicDBObject();
bo.put(
"name", "bruce");
bo.append(
"age", i);
datas.add(bo);
}
coll.insert(datas);

  单个插入:

BasicDBObject doc = new BasicDBObject();
doc.put(
"name", "MongoDB");
doc.put(
"type", "database");
doc.put(
"count", 1);
BasicDBObject info
= new BasicDBObject();
info.put(
"x", 203);
info.put(
"y", 102);
doc.put(
"info", info);
coll.insert(doc);

  6、索引

coll.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending -1 descending

  获取所有的索引:

List<DBObject> list = coll.getIndexInfo();
for (DBObject o : list) {
System.out.println(o);
}
0
相关文章