mongodb-java

Query with sort() and limit() in Spring Repository interface

ぐ巨炮叔叔 提交于 2019-11-27 19:28:32
I'm new to Spring Data with MongoDB and would like to have an automagically generated query method inside my MongoRepository extension interface which requires filtering, sorting and limiting. The query looks like this: // 'created' is the field I need to sort against find({state:'ACTIVE'}).sort({created:-1}).limit(1) The repository interface looks like this: public interface JobRepository extends MongoRepository<Job, String> { @Query("{ state: 'ACTIVE', userId: ?0 }") List<Job> findActiveByUserId(String userId); // The next line is the problem, it wont work since // it's not in the format

Case insensitive sorting in MongoDB

十年热恋 提交于 2019-11-27 12:01:16
How can I sort a MongoDB collection by a given field, case-insensitively? By default, I get A-Z before a-z. I'm using Java. Sammaye Update: As of now mongodb have case insensitive indexes: Users.find({}) .collation({locale: "en" }) .sort({name: 1}) .exec() .then(...) shell: db.getCollection('users') .find({}) .collation({'locale':'en'}) .sort({'firstName':1}) Update: This answer is out of date, 3.4 will have case insensitive indexes. Look to the JIRA for more information https://jira.mongodb.org/browse/SERVER-90 Unfortunately MongoDB does not yet have case insensitive indexes: https://jira

converting Document objects in MongoDB 3 to POJOS

拥有回忆 提交于 2019-11-27 06:40:39
问题 I'm saving an object with a java.util.Date field into a MongoDB 3.2 instance. ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(myObject); collection.insertOne(Document.parse(json)); the String contains: "captured": 1454549266735 then I read it from the MongoDB instance: final Document document = collection.find(eq("key", value)).first(); final String json = document.toJson(); ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature

How to insert multiple documents at once in MongoDB through Java

a 夏天 提交于 2019-11-27 04:12:19
I am using MongoDB in my application and was needed to insert multiple documents inside a MongoDB collection . The version I am using is of 1.6 I saw an example here http://docs.mongodb.org/manual/core/create/ in the Bulk Insert Multiple Documents Section Where the author was passing an array to do this . When I tried the same , but why it isn't allowing , and please tell me how can I insert multiple documents at once ?? package com; import java.util.Date; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.MongoClient; public class App

Mongodb avoid duplicate entries

二次信任 提交于 2019-11-27 01:16:28
问题 I am newbie to mongodb. May I know how to avoid duplicate entries. In relational tables, we use primary key to avoid it. May I know how to specify it in Mongodb using java? 回答1: Use an index with the {unique:true} option. // everyone's username must be unique: db.users.createIndex({email:1},{unique:true}); You can also do this across multiple fields. See this section in the docs for more details and examples. MongoDB indexes may optionally impose a unique key constraint , which guarantees

How to update value of specific embedded document, inside an array, of a specific document in MongoDB?

自古美人都是妖i 提交于 2019-11-26 20:02:04
问题 I have the following structure in my document: { _id : ObjectId("43jh4j343j4j"), array : [ { _arrayId : ObjectId("dsd87dsa9d87s9d7"), someField : "something", someField2 : "something2" }, { _arrayId : ObjectId("sds9a0d9da0d9sa0"), someField : "somethingElse", someField2 : "somethingElse2" } ] } I want to update someField and someField2 but only for one of the items in the array, the one that matches _arrayId (e.g. _arrayId : ObjectId("dsd87dsa9d87s9d7") ; and only for this document (e.g. _id

Case insensitive sorting in MongoDB

混江龙づ霸主 提交于 2019-11-26 15:51:41
问题 How can I sort a MongoDB collection by a given field, case-insensitively? By default, I get A-Z before a-z. I'm using Java. 回答1: Update: As of now mongodb have case insensitive indexes: Users.find({}) .collation({locale: "en" }) .sort({name: 1}) .exec() .then(...) shell: db.getCollection('users') .find({}) .collation({'locale':'en'}) .sort({'firstName':1}) Update: This answer is out of date, 3.4 will have case insensitive indexes. Look to the JIRA for more information https://jira.mongodb.org

How do I partially update an object in MongoDB so the new object will overlay / merge with the existing one

做~自己de王妃 提交于 2019-11-26 11:39:16
Given this document saved in MongoDB { _id : ..., some_key: { param1 : "val1", param2 : "val2", param3 : "val3" } } An object with new information on param2 and param3 from the outside world needs to be saved var new_info = { param2 : "val2_new", param3 : "val3_new" }; I want to merge / overlay the new fields over the existing state of the object so that param1 doesn't get removed Doing this db.collection.update( { _id:...} , { $set: { some_key : new_info } } Will lead to MongoDB is doing exactly as it was asked, and sets some_key to that value. replacing the old one. { _id : ..., some_key: {

How do I partially update an object in MongoDB so the new object will overlay / merge with the existing one

匆匆过客 提交于 2019-11-26 01:57:25
问题 Given this document saved in MongoDB { _id : ..., some_key: { param1 : \"val1\", param2 : \"val2\", param3 : \"val3\" } } An object with new information on param2 and param3 from the outside world needs to be saved var new_info = { param2 : \"val2_new\", param3 : \"val3_new\" }; I want to merge / overlay the new fields over the existing state of the object so that param1 doesn\'t get removed Doing this db.collection.update( { _id:...} , { $set: { some_key : new_info } } Will lead to MongoDB