mongdb java api 增删改查

mongdb客户端

@Configuration("meta")  
public class ConfigBeans {  
   @Value("${MONGODB_URL}")
   private String MONGODB_URL;
   @Value("${MONGODB_PORT}")
   private Integer MONGODB_PORT;
   private MongoClient mongoClient = null;

   @Bean

   public MongoClient mongoClient(){
 MongoClientOptions options = MongoClientOptions.builder().connectTimeout(10000).socketTimeout(10000).build();
 mongoClient = new MongoClient(new ServerAddress(MONGODB_URL, MONGODB_PORT), options);
 return mongoClient;
 }

 }  

mongdb增删改查

import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Sorts;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.y2game.dubbo.service.MongoService;
import org.bson.Document;
import org.elasticsearch.client.transport.TransportClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Auther: chenjiale
 * @Date: 2018/9/11 11:07
 * @Description:
 */
@Service
public class MongoServcieImpl implements MongoService {
@Autowired
private MongoClient mongoClient;

/**
 * 查询  * @param keyword  * @return
 */
@Override
public List<Map<String, Object>> searchMany(String keyword) {
    List<Map<String, Object>> list = new ArrayList<>();
    MongoCollection<Document> collection = mongoClient.getDatabase("tutorial").getCollection("movie");
    /*根据id  BasicDBObject query = new BasicDBObject("_id", new ObjectId("5a2e1e3b811e231850d2da0d"));*/
    Pattern queryPattern = Pattern.compile(keyword, Pattern.CASE_INSENSITIVE);
    BasicDBObject query = new BasicDBObject("directed_by", queryPattern);
    /* 大于小于  BasicDBObject gt = new BasicDBObject("$lt",1);
     BasicDBObject query = new BasicDBObject("grade",gt);*/
    /*查询数组
     List<String> tags=new ArrayList<>();
     tags.add("good");
      Document query = new Document("tags", new Document(QueryOperators.IN, tags));*/
    //BasicDBObject query = new BasicDBObject("directed_by",queryPattern);
    // document2.append("directed_by", "David Fincher");
    // DBObject接口和BasicDBObject对象:表示一个具体的记录,BasicDBObject实现了DBObject,是key-value的数据结构,用起来和HashMap是基本一致的。
    FindIterable<Document> iterable = collection.find(query).
    /*  sort(Sorts.descending("date")).*/
    skip(0).limit(10);
    ;
    MongoCursor<Document> cursor = iterable.iterator();
    while (cursor.hasNext()) {
        Document user = cursor.next();
        list.add(user);
    }
    return list;
}

@Override
public Boolean add() {
    Map<String, Object> areaMap3 = new HashMap<>();
    areaMap3.put("addr", "北京");
    Map<String, Object> areaMap1 = new HashMap<>();
    areaMap1.put("addr", "上海");
    List<Document> documents = new ArrayList<>();
    documents.add(new Document(areaMap3));
    documents.add(new Document(areaMap1));
    MongoCollection<Document> collection = mongoClient.getDatabase("mytest").getCollection("movie");
    long preCount = collection.count();
    collection.insertMany(documents);
    long nowCount = collection.count();
    return (nowCount - preCount) == documents.size());

}

@Override
public Boolean deleteDocument() {
    MongoCollection<Document> collection = mongoClient.getDatabase("mytest").getCollection("movie");
    BasicDBObject basicDBObject = new BasicDBObject();
    basicDBObject.append("addr", "上海");
    DeleteResult deleteManyResult = collection.deleteMany(basicDBObject);
    long deletedCount = deleteManyResult.getDeletedCount();
    return deletedCount>0;
}

@Override
public Boolean updateDocument(String id) {
    MongoCollection<Document> collection = mongoClient.getDatabase("mytest").getCollection("movie");
    Map<String, Object> updateDoc = new HashMap<String, Object>();
    Map<String, Object> wehereDoc = new HashMap<String, Object>();
    wehereDoc.put("_id", "5a2e1e3b811e231850d2da0d");
    updateDoc.put("addr", "成都");
    UpdateResult updateManyResult = collection.updateMany(new BasicDBObject(wehereDoc), new BasicDBObject("$set", updateDoc));
    // UpdateResult updateManyResult = collection.updateOne(new BasicDBObject(wehereDoc),new BasicDBObject("$set",updateDoc));
    long modifiedCount = updateManyResult.getModifiedCount();
    System.out.println("修改的数量: " + modifiedCount);
    return modifiedCount>0;
    }
}

猜你喜欢

转载自blog.csdn.net/chenjiale0102/article/details/78794461