Tag: mongodb

在MongoCollection 中找到

我有一个MongoCollection ,我在其中分配了一个集合。 我想通过他的身份找到一个用户。 user = (Document) usersCollection.find(new Document(“_id”, username)); 我得到了一个错误 java.lang.ClassCastException:com.mongodb.FindIterableImpl无法强制转换为org.bson.Document 当我尝试 BasicDBObject query = new BasicDBObject(); BasicDBObject fields = new BasicDBObject(“_id”, username); usersCollection.find(query, fields); 我收到了一个错误 MongoCollection类型中的方法find(Bson,Class)不适用于参数(BasicDBObject,BasicDBObject)

如何在mongo中构造查询来更新嵌套数组文档?

我在mongo有以下文件, { “_id” : ObjectId(“506e9e54a4e8f51423679428”), “description” : “ffffffffffffffff”, “menus” : [ { “_id” : ObjectId(“506e9e5aa4e8f51423679429”), “description” : “ffffffffffffffffffff”, “items” : [ { “name” : “xcvxc”, “description” : “vxvxcvxc”, “text” : “vxcvxcvx”, “menuKey” : “0”, “onSelect” : “1”, “_id” : ObjectId(“506e9f07a4e8f5142367942f”) } , { “name” : “abcd”, “description” : “qqq”, “text” : “qqq”, “menuKey” : “0”, […]

MongoDB返回过去一个月中每天的文档数

我有一个每天更新文件的集合。 有人可以提供一些建议,以便返回过去一个月每天添加的文件数。 我有一个带有创建时间戳的字段,如下所示..“createdTimestamp”:ISODate(“2014-03-19T19:25:23.351Z”)

在java中隐藏RFC3339 DateTime到日期

如何在RFC中将RFC 3339 com.google.api.client.util.DateTime转换为DateTime。 例如,我得到“2014-07-21T16:35:27.000Z”,我需要将其转换为“2014年7月15日下午6:07:25”格式。 无论如何转换这个? 这是我试过的。 我已将DateandTime保存为mongo db中的字符串。 public Map getYouTubeLastFetchDateTime(String key) { System.out.println(“Inserting data first time….”); Date nowDate = new Date(); Date dateBefore = new Date(nowDate.getTime() – 7 * 24 * 3600 * 1000); Utils utils = new Utils(); DBCollection collection = utils.getStaging().getCollection( DB_YOUTUBE_COLLECTION_NAME); if (null != collection) { CIPKeyWord keyword = new CIPKeyWord(); […]

MongoDB文档作为JsonNode(jackson库)

我有以下用例 – 在mongodb中存储JSON模式(动态,随时间变化)。 从文件中读取JSON对象并根据模式validation它们(在#1中) 我正在使用这个JSON Validator 。 我需要从mongo db读取模式并将其转换为JsonNode(Jackson库)。 我正在使用Java .. 任何人都可以让我知道如何将mongodb文档转换为JsonNode ..我需要这个,因为我使用的validation器(在上面的#3中提到)需要一个JsonNode来构造架构对象。 编辑:将DBObject转换为JSON字符串,然后将其转换为JsonNode是否良好的性能?

Java MongoClient无法连接到主服务器

我有一个副本集设置,1个主要(mongo1.test.com),1个辅助(mongo2.test.com)和1个仲裁(mongo3.test.com)。 当我使用MongoClient连接到它们并打印出ReplicaSetStatus时,它显示mongo1.test.com与type = Unknown无关,mongo2.test.com为type = ReplicaSetSecondary,mongo3.test.com为type = Unknown。 因为它不知道哪一个是主要的,我可以找到查询,但我无法插入或更新。 现在我不知道它是Mongo的设置还是驱动程序的配置问题。 任何建议? Mongo版本2.6.1 Java Mongo驱动程序版本2.12.1 Mongo安装在3个单独的Amazon EC2 Linux服务器上。 这是代码: MongoClientOptions.Builder optionsBuilder = MongoClientOptions.builder() .acceptableLatencyDifference(10000) .writeConcern(WriteConcern.REPLICA_ACKNOWLEDGED) .readPreference(ReadPreference.secondaryPreferred()) .connectionsPerHost(10) .connectTimeout(15000) .maxWaitTime(30000) .socketTimeout(60000) .threadsAllowedToBlockForConnectionMultiplier(1500); MongoClientOptions options = optionsBuilder.build(); MongoClient mc = new MongoClient(Arrays.asList( new ServerAddress(“mongo1.test.com”,27017), new ServerAddress(“mongo2.test.com”,27018), new ServerAddress(“mongo3.test.com”,27019)), Arrays.asList(MongoCredential.createMongoCRCredential(xxx, “admin”, xxx.toCharArray()))), options); System.out.println(mc.getRelicaSetStatus()); ReplicaSetStatus打印输出: ReplicaSetStatus { name=eeRS1, cluster=ClusterDescription{ […]

使用MongoDB的MapReduce Java驱动程序错误的BSONElement断言类型错误

我是MongoDB和MapReduce的新手。 我需要在我的数据库中的集合上做一些MapReduce。 MAP和REDUCE_MAX函数可以工作,因为我能够在Mongo交互式shell(v.1.8.2)中完成我的需求。 但是,使用Mongo Java驱动程序(v.2.6.3)尝试执行相同的操作时出错 我的MAP和REDUCE_MAX函数如下所示: String MAP = “function(){” + “if(this.type != \”checkin\”){return;}” + “if(!this.venue && !this.venue.id){return;}” + “emit({userId:this.userId, venueId:this.venue.id}, {count:1});” + “};”; String REDUCE_MAX = “function(key, values){” + “var res = {count:0};” + “values.forEach(function(value){result.count += value.count;});” + “return res;” + “};”; 这是我正在执行的命令: MapReduceOutput sum = collection .mapReduce(MAP, REDUCE_MAX, null, null); 这是我得到的错误: com.mongodb.CommandResult$CommandFailure: command […]

Hadoop HDFS MapReduce输出到MongoDb

我想编写Java程序,它从HDFS读取输入,使用MapReduce处理它并将输出写入MongoDb。 这是场景: 我有一个Hadoop集群,它有3个数据节点。 java程序从HDFS读取输入,使用MapReduce处理它。 最后,将结果写入MongoDb。 实际上,从HDFS读取并使用MapReduce处理它很简单。 但我对将结果写入MongoDb感到困惑。 是否支持将Java API写入MongoDB? 另一个问题是,由于它是一个Hadoop集群,所以我们不知道哪个datanode将运行Reducer任务并生成结果,是否可以将结果写入安装在特定服务器上的MongoDb? 如果我想将结果写入HDFS,代码将如下所示: @Override public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException { long sum = 0; for (LongWritable value : values) { sum += value.get(); } context.write(new Text(key), new LongWritable(sum)); } 现在我想将结果写入MongoDb而不是HDFS,我该怎么做?

如何使用Java驱动程序访问MongoDB中嵌套在数组中的对象

{ “_id” : 0 , “prices” : [ { “type” : “house” , “price” : 10345} , { “type” : “bed” , “price” : 456.94} , { “type” : “carpet” , “price” : 900.45} , { “type” : “carpet” , “price” : 704.48} ] } 在avobe文档中,如何使用java驱动程序删除价格最低的地毯? 即,我要删除{ “type” : “carpet” , “price” : 704.48}

MappingMongoConverter setMapKeyDotReplacement不起作用

我的代码如下链接: 如何在Spring-Boot中自定义MappingMongoConverter(setMapKeyDotReplacement)而不破坏自动配置? @Override @Bean public MappingMongoConverter mappingMongoConverter() throws Exception { DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(this.mongoDbFactory()); MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, this.mongoMappingContext()); converter.setCustomConversions(this.customConversions()); converter.setMapKeyDotReplacement(“_”); return converter; } 但是,如果我尝试解析这个JSON,即Java JSONObject: { “Dr.Web category”: “known infection source”, “categories”: [ “parked”, “uncategorized” ] } 这种例外总会发生。 org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.data.mapping.model.MappingException: Map key Dr.Web category contains dots […]