我正在尝试使用MongoDB中的某些条件(使用Java驱动程序).这就是我正在做的事情:
Pattern regex = Pattern.compile("title");
DBCollection coll = MongoDBUtil.getDB().getCollection("post_details");
BasicDBObject query = new BasicDBObject();
query.put("category_title", "myCategory");
query.append("post_title", regex);
query.append("post_description", regex);
DBCursor cur = coll.find(query);
while(cur.hasNext()) {
System.out.println(cur.next().get("post_id"));
}
Run Code Online (Sandbox Code Playgroud)
我想$or在这些条件下使用操作数,但我猜默认是"和",我不知道如何更改它.在上面的代码中,如果其中一个条件返回null,结果也是null如此.
以下是聚合查询:
[
{
"$match": {
"UserId": {
"$in": [
5
]
},
"WorkflowStartTime": {
"$gte": ISODate('2015-04-09T00:00:00.000Z'),
"$lte": ISODate('2015-04-16T00:00:00.000Z')
}
}
},
{
"$group": {
"_id": {
"Task": "$TaskId",
"WorkflowId": "$WorkflowInstanceId"
},
"TaskName": {
"$first": "$Task"
},
"StartTime": {
"$first": "$StartTime"
},
"EndTime": {
"$last": "$EndTime"
},
"LastExecutionTime": {
"$last": "$StartTime"
},
"WorkflowName": {
"$first": "$WorkflowName"
}
}
},
{
"$project": {
"_id": 1,
"LastExecutionTime": 1,
"TaskName": 1,
"AverageExecutionTime": {
"$subtract": [
"$EndTime",
"$StartTime"
]
},
"WorkflowName": 1
}
},
{ …Run Code Online (Sandbox Code Playgroud) 在早期版本的MongoDB Java驱动程序中,要运行查询并对结果执行无序批量upsert,我们所做的就是:
BulkWriteOperation bulk = dbCollection.initializeUnorderedBulkOperation();
bulk.find(searchQuery).upsert().update(new BasicDBObject("$set", getDbObjectModel()));
Run Code Online (Sandbox Code Playgroud)
但是在版本3中,随着Bson Document支持和MongoCollection.bulkWrite()方法的引入,如何才能做到这一点?
我试过这个:
List<WriteModel<Document>> documentList = new ArrayList<>();
collection.bulkWrite(documentList, new BulkWriteOptions().ordered(false));
Run Code Online (Sandbox Code Playgroud)
但是,我需要upsert功能.
谢谢.
我正在批量写入MongoDB,并且得到了OOM异常(java.lang.OutOfMemoryError:超出了GC开销限制).我会问两个问题:1.这个OOM是从Mongo客户端驱动程序还是MongoDB服务器发生的?2.是否有一些线索如何发生这种情况?
FO 2016-11-15 15:19:10,437 - [TS] org.mongodb.driver.cluster info(71) - No server chosen by WritableServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=MULTIPLE, all=[ServerDescription{address=mongo.server1-or:30000, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=mongo.server2:30000, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=mongo.server3:30000, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
INFO 2016-11-15 15:19:11,448 - [TS] org.mongodb.driver.cluster info(71) - No server chosen by WritableServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=MULTIPLE, all=[ServerDescription{address=mongo.server1-or:30000, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=mongo.server2:30000, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=mongo.server3:30000, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
INFO 2016-11-15 15:19:14,324 - [TS] org.mongodb.driver.cluster info(76) - Exception in monitor …
我试图寻找一个示例或使用ClusterListener来优化和改进与MongoDB Java客户端集成的服务的调试信息.
我们如何有效地使用Replication来改进我们的Mongo集群?
java mongodb database-replication mongodb-java mongo-java-driver
我想看看 mongo java 驱动程序产生什么查询,但我无法做到这一点。
使用官方文档中的信息,我只能在更新操作执行的日志中看到,但我没有看到此操作的查询。
MongoIterable.forEach需要一个Block与Java 8非常相似的东西Consumer.它们足够相似会导致问题,例如,以下内容无法编译:
MongoIterable<Document> result = collection.find(...);
result.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
因为编译器无法在Iterable.forEach( Consumer ) 和之间做出决定MongoIterable.forEach( Block ).修复此问题需要明确键入参数的解决方法:
Block<Document> printer = System.out::println;
result.forEach(printer);
Run Code Online (Sandbox Code Playgroud)
或者,MongoIterable作为一个平原处理Stream:
StreamSupport.stream(result.spliterator(), false).forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
为什么MongoIterable.forEach没有使用Consumer界面定义,例如:MongoIterable.forEach(Consumer<? super TResult> consumer)?更好 - 为什么要forEach进去MongoIterable呢?
我在mongo中有以下文档:
> { "_id": ObjectId("569afce4b932c542500143ec"),
> "date": "2016-1-17T2:31:0Z",
> "day": NumberInt(17),
> "model1": {
> "date": "2016-01-17T02:31+0000",
> "MondayModel": {
> "gtxdotdot": {
> "xdotdot": 0,
> "xdot": 0
> },
> "lsxdotdot": {
> "xdotdot": 0,
> "xdot": 0
> },
> "gtxdot": {
> "xdotdot": 0,
> "xdot": 0
> },
> "lsxdot": {
> "xdotdot": 0,
> "xdot": 0
> },
> "modeldotdot": {
> "mean": 0,
> "sdvar": 0
> },
> "modeldot": {
> "mean": …Run Code Online (Sandbox Code Playgroud) 我有一个更新的对象/文档列表,我需要立即保存列表中的所有对象。
我在 MongoTemplate 中看到 save() 但它一次只能保存一个文档。有什么方法可以一次保存多个文档,或者我需要调用循环保存?
mongodb mongo-java spring-data-mongodb mongotemplate mongo-java-driver
我使用MongoDB v3.2.0和Mongo Java Driver 3.0.4版本.我使用BasicDBObject(不推荐使用)而不是使用Documentjava,因为我需要做很多更改才能在我的独立java项目中转换为Document.任何人都可以告诉我更改为Document,内存和大型集合插入和读取是否会有任何性能改进.有没有办法改善我使用java在MongoDB上的频繁写入和读取操作.