mongodb-java

MongoException: Index with name: code already exists with different options

风流意气都作罢 提交于 2019-12-08 16:13:28
问题 I have a mongodb collection term with following structure { "_id" : "00002c34-a4ca-42ee-b242-e9bab8e3a01f", "terminologyClass" : "USER", "code" : "X67", "terminology" : "some term related notes", "notes" : "some notes" } and a java class representing the term collection as Term.java @Document public class Term{ @Id protected String termId; @Indexed protected String terminologyClass; @Indexed(unique=true) protected String code; @Indexed protected String terminology; protected String notes; /

inserting image into mongo from java result in a strange error

萝らか妹 提交于 2019-12-08 12:38:51
问题 I have the following code for saving image in mongodb: public static void insertImage() throws Exception { String newFileName = "mkyong-java-image"; File imageFile = new File("c:\\JavaWebHosting.png"); GridFS gfsPhoto = new GridFS(db, "photo"); GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile); gfsFile.setFilename(newFileName); gfsFile.save(); } And I got this from this link: link for code But when I use that I get the following error and I do not know how to fix it ... Can anyone help

How to access object nested inside an array in MongoDB using Java driver

孤街醉人 提交于 2019-12-08 07:53:16
问题 { "_id" : 0 , "prices" : [ { "type" : "house" , "price" : 10345} , { "type" : "bed" , "price" : 456.94} , { "type" : "carpet" , "price" : 900.45} , { "type" : "carpet" , "price" : 704.48} ] } In avobe document how'll I delete the carpet which have lowest price using java driver? i.e., I've to delete { "type" : "carpet" , "price" : 704.48} 回答1: Try this: DBCursor cursor = collection.find(new BasicDBObject("prices.type", "carpet"), new BasicDBObject("prices", 1)); try { while (cursor.hasNext())

Add multiple sub-documents in MongoDB

爷,独闯天下 提交于 2019-12-08 07:27:18
问题 I am working on a customer data loader, where customers can have multiple addresses. If the customer is not found, I create it and add the address. If the customer exists, I simply add the new address, like this: DBObject findCustomer = new BasicDBObject(); findCustomer.put("email", custEmail); //check for existing customer DBObject newCustomer = customerCollection.findOne(findCustomer); if (newCustomer == null) { //INSERT newCustomer = new BasicDBObject(); newCustomer.put("firstname",

How to retrieve all matching elements present inside array in Mongo DB?

痞子三分冷 提交于 2019-12-08 06:39:59
问题 I have document shown below: { name: "testing", place:"London", documents: [ { x:1, y:2, }, { x:1, y:3, }, { x:4, y:3, } ] } I want to retrieve all matching documents i.e. I want o/p in below format: { name: "testing", place:"London", documents: [ { x:1, y:2, }, { x:1, y:3, } ] } What I have tried is : db.test.find({"documents.x": 1},{_id: 0, documents: {$elemMatch: {x: 1}}}); But, it gives first entry only. 回答1: As JohnnyHK said, the answer in MongoDB: select matched elements of

Java driver equivalent for JavaScript shell's Object.bsonsize( doc )?

时光毁灭记忆、已成空白 提交于 2019-12-07 20:05:15
问题 I was wondering what the Java driver's equivalent to the Mongo JavaScript shell's Object.bsonsize( doc ) method? For example, what is the Java code to perform the following: bobk-mbp:~ bobk$ mongo MongoDB shell version: 2.0.4 connecting to: test PRIMARY> use devices; switched to db devices PRIMARY> Object.bsonsize( db.profiles.findOne( { _id: "REK_0001" } ) ); 186 PRIMARY> Object.bsonsize( db.profiles.findOne( { _id: "REK_0002" } ) ); 218 PRIMARY> How do I perform this same basic use case

Connecting to a MongoDB with Android in Eclipse

ぐ巨炮叔叔 提交于 2019-12-07 16:07:03
问题 I am currently developing an Android application, which is going to use a database from MongoLab . Another member of my team has already created a working database on mongolab.com , I am just having trouble connecting to it via Eclipse. I have looked all around Google and at countless tutorials, but I really don't know much about what I'm doing. The best tutorial I have found is from mkyong.com. It seems the second section labeled Java MongoDB Examples is what I am looking for, but I cannot

How to convert BigDecimal to Double in spring-data-mongodb framework

若如初见. 提交于 2019-12-07 05:11:15
问题 Spring Data MongoDB mapping by default converts BigDecimal to String. However, I want them to be converted as Double in mongodb. This is required for latter to make queries on this field in mongodb (comparison queries/aggregation queries). How can I reigster my own converter (BigDecimalToDouble / DoubleToBigDecimal) to do this? 回答1: Here's how you can add your own converters: <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref=

Creating a mongodb query in Java with $or and $in

人走茶凉 提交于 2019-12-07 04:56:41
问题 I am trying to write a java code using mongodb api to create this mongodb query: { "$or": [{"prd" : {"$in" : ["1234", "0987"]}} , {"rsin" : "3228742"}]} Here's the code I am working with so far: QueryBuilder builder = new QueryBuilder(); if (builder == null) { builder = QueryBuilder.start(); } if (mongoKey.equals("prd")){ ArrayList<String> vals = new ArrayList<String>(); for (int i=0; i < prdList; i++){ vals.add(prdList.get(i)); } DBObject obj = new BasicDBObject (mongoKey, new BasicDBObject(

Concurrency - Getting the MongoDB generated ID of an object inserted via Java in a thread safe way

喜你入骨 提交于 2019-12-07 01:13:38
问题 What is the best method to get the Mongo generated ID of a document inserted via Java. The Java process inserting the documents is multi-thread, meaning that we need some atomic way to insert and return the ID of the object. Also, if we setup a unique index, in the event that the object is a duplicate, will an ID be returned? Thanks! 回答1: Generate the ObjectId early, use it in the insert, and there will no need to have the database return it to you. ObjectId doesn't use a shared sequence