本文整理汇总了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);
}