本文整理匯總了Java中com.mongodb.client.MongoCollection.deleteOne方法的典型用法代碼示例。如果您正苦於以下問題:Java MongoCollection.deleteOne方法的具體用法?Java MongoCollection.deleteOne怎麽用?Java MongoCollection.deleteOne使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.mongodb.client.MongoCollection
的用法示例。
在下文中一共展示了MongoCollection.deleteOne方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: deleteById
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
/**
* 刪除記錄
*
* @param collectionName
* 表名
* @param mongoObj
* 記錄
* @return
*/
public static boolean deleteById(String collectionName, MongoObj mongoObj) {
MongoCollection<Document> collection = getCollection(collectionName);
try {
Bson filter = Filters.eq(MongoConfig.MONGO_ID, mongoObj.getDocument().getObjectId(MongoConfig.MONGO_ID));
DeleteResult result = collection.deleteOne(filter);
if (result.getDeletedCount() == 1) {
return true;
} else {
return false;
}
} catch (Exception e) {
if (log != null) {
log.error("刪除記錄失敗", e);
}
return false;
}
}
示例2: removeClient
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
@RequestMapping(NectarServerApplication.ROOT_PATH + "/auth/removeClient")
public ResponseEntity removeClient(@RequestParam(value = "token") String jwtRaw, @RequestParam(value = "uuid") String uuid,
HttpServletRequest request) {
ResponseEntity r = Util.verifyJWT(jwtRaw, request);
if(r != null)
return r;
ManagementSessionToken token = ManagementSessionToken.fromJSON(Util.getJWTPayload(jwtRaw));
if(token == null)
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid TOKENTYPE.");
if(SessionController.getInstance().checkManagementToken(token)) {
MongoCollection<Document> clients = NectarServerApplication.getDb().getCollection("clients");
Document client = clients.find(Filters.eq("uuid", uuid)).first();
if(client == null) // Check if the client exists
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Client not found in database.");
if(!(client.getOrDefault("loggedInUser", "null").equals("null"))) { // Check if a user is currently signed into the client.
NectarServerApplication.getLogger().warn("Attempted client deletion from " + request.getRemoteAddr() + ", a user is already signed into client " + uuid);
return ResponseEntity.status(HttpStatus.CONFLICT).body("A user is currently signed into this client.");
}
if(SessionController.getInstance().sessions.containsKey(uuid)) { // Check if the client is currently online with a session open
SessionController.getInstance().sessions.remove(uuid); // Remove the session and it's token
NectarServerApplication.getLogger().info("Revoked token for " + uuid + ": client deleted");
}
clients.deleteOne(Filters.eq("uuid", uuid)); // Delete client from the MongoDB database
NectarServerApplication.getEventLog().logEntry(EventLog.EntryLevel.NOTICE, "Deleted client " + uuid + ", traced from " + request.getRemoteAddr());
} else {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("Token expired/not valid.");
}
return ResponseEntity.status(HttpStatus.NO_CONTENT).body("Success.");
}
示例3: deleteById
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
public static int deleteById(MongoCollection<Document> col, Object id) {
int count = 0;
Bson filter = Filters.eq("_id", id);
DeleteResult deleteResult = col.deleteOne(filter);
count = (int) deleteResult.getDeletedCount();
return count;
}
示例4: deleteEntry
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
/**
* deletes an entry from the DB
* @param entry to delete
*/
public static void deleteEntry(DBEntry entry) {
if(entry == null)
return;
MongoDatabase db = MongoDB.INSTANCE.getDatabase("scraping");
String collectionName = getCollectionName(entry);
MongoCollection collection = db.getCollection(collectionName, BasicDBObject.class);
collection.deleteOne(eq("_id",entry.getId()));
}
示例5: id
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
@Then("^Delete lead with id (\\d+) in the database")
public void Delete_lead_with_id_in_intellead_data_mongodb_database(int leadId) {
MongoDatabase database = mongoClientData.getDatabase("local");
MongoCollection<Document> collection = database.getCollection("leads");
collection.deleteOne(parse("{_id: {$eq: \"" + leadId + "\"}}"));
long count = collection.count(parse("{_id: {$eq: \"" + leadId + "\"}}"));
assertEquals(0, count);
}
示例6: main
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
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());
}
}
示例7: removeUser
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
@RequestMapping(NectarServerApplication.ROOT_PATH + "/auth/removeUser")
public ResponseEntity removeUser(@RequestParam(value = "token") String jwtRaw, @RequestParam(value = "user") String username, HttpServletRequest request) {
ResponseEntity r = Util.verifyJWT(jwtRaw, request);
if(r != null)
return r;
ManagementSessionToken token = ManagementSessionToken.fromJSON(Util.getJWTPayload(jwtRaw));
if(token == null)
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid TOKENTYPE.");
if(SessionController.getInstance().checkManagementToken(token)) {
MongoCollection<Document> users = NectarServerApplication.getDb().getCollection("users");
MongoCollection<Document> clients = NectarServerApplication.getDb().getCollection("clients");
// Check that the user exists
Document clientDoc = users.find(Filters.eq("username", username)).first();
if(clientDoc == null)
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Username not found in database!");
// Check that the user is not signed in
FindIterable<Document> clientsWithUserSignedIn = clients.find(Filters.eq("loggedInUser", username));
if(clientsWithUserSignedIn.first() != null)
return ResponseEntity.status(HttpStatus.CONFLICT).body("The user is currently signed into a client!");
// Delete the user entry in the database
users.deleteOne(Filters.eq("username", username));
// Remove the user's FTS store
File storeLocation = new File(NectarServerApplication.getConfiguration().getFtsDirectory() + File.separator
+ "usrStore" + File.separator + username
);
try {
FileUtils.deleteDirectory(storeLocation);
} catch (IOException e) {
NectarServerApplication.getLogger().warn("Failed to delete FTS store for former user \"" + username + "\"");
NectarServerApplication.getEventLog().addEntry(EventLog.EntryLevel.WARNING, "Failed to delete FTS store while deleting user " + username);
}
NectarServerApplication.getEventLog().logEntry(EventLog.EntryLevel.INFO, "Removed user \"" + username + "\" by MANAGEMENT SESSION: " + token.getClientIP());
} else {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("Token expired/not valid.");
}
return ResponseEntity.status(HttpStatus.NO_CONTENT).body("Success.");
}
示例8: deleteRegionData
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
public void deleteRegionData(LoadedRegion region) {
MongoCollection<Document> collection = getDatabase().getCollection("chunks");
Bson condition = new Document("_id", region.getUniqueId().toString());
collection.deleteOne(condition);
}
示例9: deleteUserData
import com.mongodb.client.MongoCollection; //導入方法依賴的package包/類
public void deleteUserData(UserData userData) {
MongoCollection<Document> collection = getDatabase().getCollection("users");
Bson condition = new Document("_id", userData.getUniqueId().toString());
collection.deleteOne(condition);
}