Java Code Examples for com.mongodb.client.MongoCollection.deleteMany()

The following are Jave code examples for showing how to use deleteMany() of the com.mongodb.client.MongoCollection class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: mongodb-crud   File: DeleteDocumentsImpl.java   View Source Code Vote up 8 votes
/**
 * This is deleted delete all document(s), which is matched
 */
@Override
public void deleteManyDocument() {
	MongoDatabase db = null;
	MongoCollection collection = null;
	Bson query = null;
	try {
		db = client.getDatabase(mongo.getDataBase());
		collection = db.getCollection(mongo.getSampleCollection());
		query = lt("age", 20);
		DeleteResult result = collection.deleteMany(query);
		if (result.wasAcknowledged()) {
			log.info("Document deleted successfully \nNo of Document(s) Deleted : "
					+ result.getDeletedCount());
		}
	} catch (MongoException e) {
		log.error("Exception occurred while delete Many Document : " + e, e);
	}
}
 
Example 2
Project: mongodb-crud   File: DeleteDocumentsImpl.java   View Source Code Vote up 7 votes
/**
 * This is delete the single document, which is first matched
 */
@Override
public void deleteOneDocument() {
	MongoDatabase db = null;
	MongoCollection collection = null;
	Bson query = null;
	try {
		db = client.getDatabase(mongo.getDataBase());
		collection = db.getCollection(mongo.getSampleCollection());
		query = eq("name", "sundar");
		DeleteResult result = collection.deleteMany(query);
		if (result.wasAcknowledged()) {
			log.info("Single Document deleted successfully \nNo of Document Deleted : " + result.getDeletedCount());
		}
	} catch (MongoException e) {
		log.error("Exception occurred while delete Single Document : " + e, e);
	}
}
 
Example 3
Project: digital-display-garden-iteration-3-sixguysburgers-fries   File: ExcelParser.java   View Source Code Vote up 5 votes
public static void setLiveUploadId(String uploadID){

        MongoClient mongoClient = new MongoClient();
        MongoDatabase test = mongoClient.getDatabase("test");
        MongoCollection configCollection = test.getCollection("config");

        configCollection.deleteMany(exists("liveUploadId"));
        configCollection.insertOne(new Document().append("liveUploadId", uploadID));
    }
 
Example 4
Project: digital-display-garden-iteration-4-dorfner-v2   File: ExcelParser.java   View Source Code Vote up 5 votes
public void setLiveUploadID(String uploadID){

        MongoClient mongoClient = new MongoClient();
        MongoDatabase test = mongoClient.getDatabase(databaseName);
        MongoCollection configCollection = test.getCollection("config");

        configCollection.deleteMany(exists("liveUploadID"));
        configCollection.insertOne(new Document().append("liveUploadID", uploadID));
    }
 
Example 5
Project: jspider   File: MongodbTest.java   View Source Code Vote up 4 votes
public static void main(String[] args) {
        try {
            // 连接到 mongodb 服务
            MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

            // 连接到数据库
            MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol");
            System.out.println("Connect to database successfully");

            MongoCollection<Document> collection = mongoDatabase.getCollection("test");
            System.out.println("集合 test 选择成功");


//            //插入文档
//            /**
//             * 1. 创建文档 org.bson.Document 参数为key-value的格式
//             * 2. 创建文档集合List<Document>
//             * 3. 将文档集合插入数据库集合中 mongoCollection.insertMany(List<Document>) 插入单个文档可以用 mongoCollection.insertOne(Document)
//             * */
//            Document document = new Document("title", "MongoDB").
//                    append("description", "database").
//                    append("likes", 100).
//                    append("by", "Fly");
//            List<Document> documents = new ArrayList<Document>();
//            documents.add(document);
//            collection.insertMany(documents);
//            System.out.println("文档插入成功");

//            //检索所有文档
//            /**
//             * 1. 获取迭代器FindIterable<Document>
//             * 2. 获取游标MongoCursor<Document>
//             * 3. 通过游标遍历检索出的文档集合
//             * */
//            FindIterable<Document> findIterable = collection.find();
//            MongoCursor<Document> mongoCursor = findIterable.iterator();
//            while(mongoCursor.hasNext()){
//                System.out.println(mongoCursor.next());
//            }


//            //更新文档   将文档中likes=100的文档修改为likes=200
//            collection.updateMany(Filters.eq("likes", 100), new Document("$set",new Document("likes",200)));
//            //检索查看结果
//            FindIterable<Document> findIterable = collection.find();
//            MongoCursor<Document> mongoCursor = findIterable.iterator();
//            while(mongoCursor.hasNext()){
//                System.out.println(mongoCursor.next());
//            }


            //删除符合条件的第一个文档
            collection.deleteOne(Filters.eq("likes", 200));
            //删除所有符合条件的文档
            collection.deleteMany (Filters.eq("likes", 200));
            //检索查看结果
            FindIterable<Document> findIterable = collection.find();
            MongoCursor<Document> mongoCursor = findIterable.iterator();
            while(mongoCursor.hasNext()){
                System.out.println(mongoCursor.next());
            }


        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
    }
 
Example 6
Project: dooo   File: MongodbDataAccess.java   View Source Code Vote up 2 votes
/**
 * 按条件删除
 *
 * @param collectionName 集合名
 * @param query          查询条件
 * @return
 */
public boolean delete(String collectionName, MongodbQuery query) {
    MongoCollection collection = sMongoDatabase.getCollection(collectionName);
    DeleteResult deleteResult = collection.deleteMany(query.getQuery());
    return deleteResult.getDeletedCount() > 0;
}