Tag: mongo java driver

java代码在编译时在mongodb中给出错误

我是mongodb的新手,我有以下代码 import com.mongodb.*; import com.mongodb.Block; import com.mongodb.client.AggregateIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import org.bson.types.ObjectId; import static java.util.Arrays.asList; public class getAssets{ public static void main( String args[] ){ Block printBlock = new Block() { @Override public void apply(final Document document) { System.out.println(document.toJson()); } }; MongoClient mongoClient = new MongoClient(“localhost”,27017); MongoDatabase database = mongoClient.getDatabase(“test”); System.out.println(“Connect to […]

使用Java MongoDb驱动程序的Bson漂亮打印

我使用Java MongoDB驱动程序3.3版使用Mongo聚合框架。 我有一个聚合管道 ,它只是List类型的集合。 我试图找到一种方法来打印管道的每个阶段。 在每个元素上调用toString方法是不够的,因为每个阶段都是Bson接口的简单实现的实例,即SimplePipelineStage 。 这个愚蠢的类没有任何toString方法的重写。 使用mongo java驱动程序Aggregates类的工厂方法创建管道,如下所示: Aggregates.match(/* … */) Aggregates.project(/* … */) // And so on… Javadoc可以在这里找到。 我怎么能打印这样的物体? 我确信BasicDbObject类型有一个智能的toString实现,但我找不到从Bson转换为Bson的BasicDbObject 。 非常感谢提前。

如何使用Java在MongoDB中使用AND和OR子句执行查询

我想在MongoDB 3.2中使用Java Driver 3.2执行查询,它同时包含$and和$or子句。 通过参考 ,我尝试了以下方法: List criteria1 = new ArrayList(); List criteria2 = new ArrayList(); criteria1.add(new Document(“fetchStatus”, new Document(“$gte”, FetchStatus.PROCESSED_NLP.getID()))); criteria1.add(new Document(“fetchStatus”, new Document(“$lte”, fetchStatusParam))); criteria1.add(new Document(“episodeID”, new Document(“$in”, episodeIDs))); criteria2.add(new Document(“fetchStatus”, new Document(“$eq”, PROCESSED_FETCH.getID()))); criteria2.add(new Document(“isFullTextRet”, new Document(“$eq”, false))); BasicDBList or = new BasicDBList(); or.add(criteria1); or.add(criteria2); DBObject query = new BasicDBObject(“$or”, or); ArrayList […]

使用MongoDB Java 3.0驱动程序批量Upsert

在早期版本的MongoDB Java驱动程序中,要运行查询并对结果进行无序批量upsert,我们所做的就是: BulkWriteOperation bulk = dbCollection.initializeUnorderedBulkOperation(); bulk.find(searchQuery).upsert().update(new BasicDBObject(“$set”, getDbObjectModel())); 但是在版本3中,随着Bson Document支持和MongoCollection.bulkWrite()方法的引入,如何才能做到这一点? 我试过这个: List<WriteModel> documentList = new ArrayList(); collection.bulkWrite(documentList, new BulkWriteOptions().ordered(false)); 但是,我需要upsertfunction。 谢谢。