Tag: mongodb

在MongoDB中的事务

我正在使用带有Java和Spring Data的NoSQL数据库MongoDB。 我知道MongoDB只支持单个文档的事务。 我正在使用Spring Transactions来执行MongoDB转换。 我正在使用TransactionTemplate。 使用TransactionTemplate时,我应该在TransactionManager中设置什么? 编辑 我有这样的事情: 我需要定义txnManagerBean以指向类似于MongoDB数据库的DataSourceTransactionManager。

Java,MongoDB:如何在迭代庞大的集合时更新每个对象?

我收集了大约100万条记录,每条记录有20个字段。 我需要在每个记录(文档)中更新整数flag字段,将1或2随机分配给此flag字段。 如何在完整集合上迭代光标时执行此操作? 为了能够更新它,第二次搜索MongoDB已找到的对象似乎不是一个好主意: DBCursor cursor = coll.find(); try { while(cursor.hasNext()) { BasicDBObject obj = (BasicDBObject) cursor.next(); … coll.update(query,newObj) } } finally { cursor.close(); } 如何有效地更新具有不同值的巨大MongoDB集合的每个文档中的字段?

如何使用mongodb-java-driver进行upsert

如何使用java-driver将数据插入mongodb集合? 我尝试(空收集): db.getCollection(collection).update(new BasicDBObject(“_id”, “12”), dbobject, true, false); 但文档是使用_id == ObjectID(…)创建的。 不是“12”值。 此代码(js)按预期添加_id =“12”的文档 db.metaclass.update( { _id:12}, { $set: {b:1} }, { upsert: true } ) 蒙戈-java的驱动程序2.11.2

如何配置spring-data-mongodb以通过属性使用副本集

我目前正在编写一个应该使用MongoDB副本的应用程序。 它是一个基于Spring Boot的应用程序,以下属性可以很好地连接到一台服务器: spring.data.mongodb.host=localhost spring.data.mongodb.port=27017 spring.data.mongodb.database=demo 这对我的本地开发环境来说绝对没问题。 但是后来它应该针对MongoDB副本集运行,所以我必须提供至少2个,更好的3个副本集种子,但我怎么能用属性来做呢? 我在这个页面上看了一下: http : //docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html ,但是没有提到的副本集的显式属性。 提供以逗号分隔的地址列表,如下所示: spring.data.mongodb.host=127.0.0.1,127.0.1.1,127.0.2.1 spring.data.mongodb.uri=mongo://127.0.0.1,mongo://127.0.0.1:27018 (我一个接一个地试过。) 这也不起作用(实际上,它会产生一个exception,让Spring使用默认配置)。 我也尝试使用以下config.xml,没有运气: 我知道上面的配置略有不同,但我目前正在尝试的是获得一个exception,它向我显示没有可访问的副本集节点。 任何想法,提示?

Spring数据mongo在Query中使用OR

我们来看看有人可以帮忙解决这个问题。 我想使用Spring Data mongodb的Repository,我希望使用Query annotation按值A=10 or A=20过滤find @Query(“{A: 10, A:20}”) findById(int id); 勉强“,”尝试制作一个AND,我需要一个OR。 有什么想法吗?

使用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。 谢谢。

MongoDB嵌入式对象没有ID(空值)

我对使用Spring Data的MongoDB有疑问。 我有这些域类: @Document public class Deal { @Id private ObjectId _id; private Location location; private User user; private String description; private String title; private String price; private boolean approved; private Date expirationDate; private Date publishedDate; } @Document public class Location { @Id private ObjectId _id; private Double latitude; private Double longitude; private String country; […]

使用Java将Java对象插入MongoDB集合

我试图使用Java将整个Java对象插入到MongoDB集合中。 我收到以下错误: 错误: Exception in thread “main” java.lang.IllegalArgumentException: can’t serialize class net.yogesh.test.Employee at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:185) at org.bson.BSONEncoder.putObject(BSONEncoder.java:119) at org.bson.BSONEncoder.putObject(BSONEncoder.java:65) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:176) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:134) at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:129) at com.mongodb.DBCollection.save(DBCollection.java:418) at net.yogesh.test.test.main(test.java:31) Emplyoee.java(POJO) package net.yogesh.test; import java.io.Serializable; public class Employee implements Serializable { private static final long serialVersionUID = 1L; private long no; private String name; public Employee() { […]

java – MongoDB + Solr表演

我一直在四处寻找如何将MongoDB与Solr结合使用,这里的一些问题有部分反应,但没有什么真正具体的(更像是理论)。 在我的应用程序中,我将在MongoDB中存储大量的文档(可能高达数亿),我想对这些文档的某些属性实现全文搜索,所以我猜Solr是最好的方法这个。 我想知道的是我应该如何配置/执行所有内容以使其具有良好的性能? 现在,这就是我做的事(我知道它不是最优的): 1-在MongoDB中插入对象时,我将其添加到Solr SolrServer server = getServer(); SolrInputDocument document = new SolrInputDocument(); document.addField(“id”, documentId); … server.add(document); server.commit(); 2-当更新对象的属性时,由于Solr不能只更新一个字段,首先我从MongoDB中检索对象然后用对象和新属性的所有属性更新Solr索引并执行类似的操作 StreamingUpdateSolrServer update = new StreamingUpdateSolrServer(url, 1, 0); SolrInputDocument document = new SolrInputDocument(); document.addField(“id”, documentId); … update.add(document); update.commit(); 3-查询时,首先我查询Solr,然后在检索文档列表SolrDocumentList我会浏览每个文档,并且: 获取文档的ID 从MongoDB获取具有相同id的对象,以便能够从那里检索属性 4-删除时,我还没有完成那部分,并且不确定如何在Java中完成 那么有人建议如何以更有效的方式为这里描述的每个场景做到这一点? 喜欢这样做的过程,当在Solr中有大量文档并一次添加一个文档时,它不需要1小时来重建索引? 我的要求是用户可能希望一次添加一个文档,我希望他们能够立即检索它

如何检查MongoDB中是否存在字段?

我已经创建了一些文档并设法进行了一些简单的查询,但是我无法创建一个查询来查找字段存在的文档。 例如,假设这是一个文档: { “profile_sidebar_border_color” : “D9B17E” , “name” : “???? ???????” , “default_profile” : false , “show_all_inline_media” : true , “otherInfo”:[“text”:”sometext”, “value”:123]} 现在我想要一个查询,它将带来otherInfo中的文本所在的所有文档。 如果没有文本,那么otherInfo将是这样的: “otherInfo”:[] 所以我想检查otherInfo是否存在text字段。 我怎样才能做到这一点?