MongoDB教程
作者: 时海
CRUD

1、新增数据


MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://ip:port/fdsp-graph"));
        MongoDatabase mongoDatabase = mongoClient.getDatabase("dbName");
        MongoCollection<Document> collection = mongoDatabase.getCollection("collectionName");
        
        BasicDBObject filter = new BasicDBObject("_id", "xxx");
        FindIterable<Document> findIterable = collection.find(filter).limit(1);
        MongoCursor<Document> mongoCursor = findIterable.iterator();
        while (mongoCursor.hasNext()) {
            Document doc = mongoCursor.next();
            System.out.println(doc);
        }
        
        mongoClient.close();


2、查看数据

3、修改数据

4、删除数据

5、不存在则新增,存在则修改

 Document document = new Document("_id", id).append("data", data).append("createTime", new Date());
        Document filter = new Document();
        filter.append("_id", id);
        collection.replaceOne(filter, document, new UpdateOptions().upsert(true));

6、


标签: document、mongoclient、mongocursor、finditerable、mongodatabase
一个创业中的苦逼程序员
  • 回复
隐藏