3.0MongoDB对一个collection的常用操作

获取数据操作: 
MongoDatabase database = mongoClient.getDatabase("testdb");
获取一个collection:
MongoCollection<Document> collection = database.getCollection("test");

1、插入一条数据:
命令:
db.test.insert(
"name"  :  "MongoDB" "type"  :  "database" "count"  :  1 "info"  : { x :  203 , y :  102  } }

);
java:
 Document doc = new Document("name", "MongoDB")
                       .append("type", "database")
                       .append("count", 1)
                       .append("info", new Document("x", 203).append("y", 102));

collection.insertOne(doc);

2、批量插入数据:

List<Document> documents = new ArrayList<Document>();
 Document doc = new Document("name", "MongoDB")
                       .append("type", "database")
                       .append("count", 1)
                       .append("info", new Document("x", 203).append("y", 102));

        for (int i = 0; i < 100; i++) {
            documents.add(doc );
        }
 collection.insertMany(documents);

3、查询所有记录
 MongoCursor<Document> cursor = collection.find().iterator();
        try {
            while (cursor.hasNext()) {
                System.out.println(cursor.next().toJson());
            }
        } finally {
            cursor.close();
        }

4、查询符合相等条件的记录
cursor =collection.find(eq("i", 71))

5、查询大于、小于、等于、大于等于、小于等于某一条件的记录,以及查询某一范围的记录
cursor = collection.find(gt("i", 50)).iterator();
cursor = collection.find(new Document("i",new Document("$gt",50))).iterator();

cursor = collection.find(lt("i", 50)).iterator();
cursor = collection.find(new Document("i",new Document("$lt",50))).iterator();

cursor = collection.find(gte("i", 50)).iterator();
cursor = collection.find(new Document("i",new Document("$gte",50))).iterator();

cursor = collection.find(lte("i", 50)).iterator();
cursor = collection.find(new Document("i",new Document("$lte",50))).iterator();

cursor = collection.find(and(gt("i", 50), lte("i", 100))).iterator();
cursor = collection.find(new Document("i",new Document("$lte",50).append("$gt",40))).iterator();

6、将查询结果放在set中
 Block<Document> printBlock = new Block<Document>() {
            @Override
            public void apply(final Document document) {
                System.out.println(document.toJson());
            }
        };
        collection.find(gt("i", 50)).forEach(printBlock);

7、对查询结果排序
        myDoc = collection.find(exists("i")).sort(descending("i")).first();
或者myDoc = collection.find(exists("i")).sort(new Document("i",-1)).first();
1或者-1表示升序或者降序

8、定制返回的字段
 myDoc = collection.find().projection(excludeId()).first(); //不返回id
 myDoc = collection.find().projection(new Document("name",1).append("type",0)).first();//不返回type,返回name

9、更新文档,MongoDB有很多修改器
更新一个文档: collection.updateOne(eq("i", 10), new Document("$set", new Document("i", 110)));//将i=10的记录中i设置为110

更新多个文档:
        UpdateResult updateResult = collection.updateMany(lt("i", 100),
         new Document("$inc", new Document("i", 100)));//将i<100的记录i都加上100
        System.out.println(updateResult.getModifiedCount());

10、删除文档
 collection.deleteOne(eq("i", 110));
 DeleteResult deleteResult = collection.deleteMany(gte("i", 100));

11、批量操作
List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>();
        writes.add(new InsertOneModel<Document>(new Document("_id", 4)));
        writes.add(new InsertOneModel<Document>(new Document("_id", 5)));
        writes.add(new InsertOneModel<Document>(new Document("_id", 6)));
        writes.add(new UpdateOneModel<Document>(new Document("_id", 1), new Document("$set", new Document("x", 2))));
        writes.add(new DeleteOneModel<Document>(new Document("_id", 2)));
        writes.add(new ReplaceOneModel<Document>(new Document("_id", 3), new Document("_id", 3).append("x", 4)));

        collection.bulkWrite(writes);//顺序执行
          collection.bulkWrite(writes,new BulkWriteOptions().ordered(false));//不保证按照顺序执行
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值