我正在尝试按照此post在Java中创建“包含”查询。问题是它给了我一个错误。
目前我有以下代码:
MongoClient mongoClient = null;
DBCursor cursor = null;
mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB("bd-films");
DBCollection coll = db.getCollection("films");
DBObject query = new BasicDBObject({"title" : {$regex : "name"}});
cursor = coll.find(query);
它给我的错误是在'查询'行:
Syntax error on token ""title"", invalid Label
Syntax error, insert ";" to complete Statement
Syntax error on token ")", delete this token
我使用光标将其插入JTable
驱动程序:mongo-java-driver-3.4.2
提前谢谢。
答案 0 :(得分:1)
对java驱动程序3.x版本使用新的Document
和MongoCollection
api。
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase db = mongoClient.getDatabase("bd-films");
MongoCollection<Document> coll = db.getCollection("films");
List<Document> results = coll.find(Filters.regex("title", "name")).into(new ArrayList<>())
访问MongoCursor
,
FindIterable<Document> results = coll.find(Filters.regex("title", "name"));
MongoCursor<Document> cursor = results.iterator();