题目列表
在上面需要增加一个挑战问题,支持模糊查询。
java 版本实现:
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import org.bson.*;
import com.mongodb.*;
import com.mongodb.client.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.regex.Pattern;
// 按照这篇文章来实现的关闭 IDEA 控制台上的mongoclient 的 debug 日志
// https://stackoverflow.com/questions/30137564/how-to-disable-mongodb-java-driver-logging
public class PushingNotesv4 {
Scanner sc = new Scanner(System.in);
String note ;
int controlsiginal=0;
String mongoUri = "mongodb://127.0.0.1:27017";
MongoClientURI connStr = new MongoClientURI(mongoUri);
MongoClient mongoClient = new MongoClient(connStr);
String info ="";
void controllogic()
{
this.Get_input();
if(this.controlsiginal == 1)
{
this.insert_mongo();
}
else if (this.controlsiginal == 2)
{
this.display_mongo();
}
else if (this.controlsiginal == 3)
{
this.Search_mongo();
}
else{
//do nothing
}
}
void insert_mongo()
{
try {
// 使用名为 someonedb 的数据库
MongoDatabase database = mongoClient.getDatabase("57challenge");
// 取得集合/表 someonetable 句柄
MongoCollection<Document> collection = database.getCollection("note");
// 1.准备时间
SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
sdf.applyPattern("yyyy-MM-dd");// a为am/pm的标记
Date date = new Date();
String time = sdf.format(date);
//2.插入时间和内容
Document doc = new Document();
doc.append("time", time);
doc.append("note", this.note);
// 3. 写入数据
collection.insertOne(doc);
//System.out.println("insert document: " + doc);
} finally {
//关闭连接
mongoClient.close();
}
}
void display_mongo() {
try {
// 使用名为 someonedb 的数据库
MongoDatabase database = mongoClient.getDatabase("57challenge");
// 取得集合/表 someonetable 句柄
MongoCollection<Document> collection = database.getCollection("note");
MongoCursor<Document> cursor = collection.find().iterator();
while (cursor.hasNext()) {
//System.out.println(cursor.next());
Document a = cursor.next();
System.out.println(a.get("time") + "--" + a.get("note"));
}
}finally {
//关闭连接
mongoClient.close();
}
}
void Search_mongo(){
try {
// 使用名为 someonedb 的数据库
MongoDatabase database = mongoClient.getDatabase("57challenge");
// 取得集合/表 someonetable 句柄
MongoCollection<Document> collection = database.getCollection("note");
Pattern pattern = Pattern.compile("^.*"+this.info+".*#34;,Pattern.CASE_INSENSITIVE);
BasicDBObject query = new BasicDBObject();
query.put("note",pattern);
MongoCursor<Document> cursor = collection.find(query).iterator();
while (cursor.hasNext()) {
//System.out.println(cursor.next());
Document a = cursor.next();
System.out.println(a.get("time") + "--" + a.get("note"));
}
}finally {
//关闭连接
mongoClient.close();
}
}
void Get_input(){
System.out.println("请输入数据,以这个模式输入: mynotes new 日记 或者 mynotes show ");
String content = sc.nextLine();
String pattern = "^mynotes new.*";
String pattern2 = "^mynotes show.*";
String pattern3 = "^mynotes search.*";
if(Pattern.matches(pattern,content)) {
// 1写入逻辑
// System.out.println("这是插入逻辑");
this.note = content.replaceFirst("mynotes new","");
//System.out.println(this.note);
this.controlsiginal = 1;
}
else if(Pattern.matches(pattern2,content))
{
//2.查询逻辑
System.out.println("这是查询逻辑");
this.controlsiginal = 2;
}
else if(Pattern.matches(pattern3,content))
{
//3.模糊查询逻辑
System.out.println("这是模糊查询逻辑");
this.info = content.replaceFirst("mynotes search","").replaceFirst("^ ","");
this.controlsiginal = 3;
}
else{
System.out.println("输入有错误,请重新输入\n 输入: mynotes new 日记 或者 mynotes show ");
}
}
public static void main(String[] args) {
// ((LoggerContext) LoggerFactory.getILoggerFactory()).getLogger("org.mongodb.driver").setLevel(Level.ERROR);
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger rootlogger = loggerContext.getLogger("org.mongodb.driver");
rootlogger.setLevel(Level.ERROR);
while(true){
PushingNotesv4 lesson50 = new PushingNotesv4();
//lesson50.connect_mongo();
// lesson50.insert_db();
lesson50.controllogic();
}
}
}
主要这三列做了修改
- Pattern pattern = Pattern.compile("^.*"+this.info+".*#34;,Pattern.CASE_INSENSITIVE);
- BasicDBObject query = new BasicDBObject();
- query.put("note",pattern);
- MongoCursor<Document> cursor = collection.find(query).iterator();
在FIND 里面增加一个查询条件。
效果图: