mongodb-java

Check MongoDB server is running and credentials are valid in Java

試著忘記壹切 提交于 2019-12-03 03:51:50
I am programming an UI where a user should be able to put in the URL and port to check whether a mongoDB server is running. Furthermore he should be able to provide credentials when necessary. If the server is not running or the credentials are wrong, I want to provide a message for each case. Similar questions have been answered here: Check MongoDB authentication with Java 3.0 driver how to check from a driver, if mongoDB server is running Unfortunately they use older versions of the Java driver for mongo. I'm using the 3.2+ version of the MongoDB java driver, where i.e. getDB() is deprecated

Authentication with Spring Security + Spring data + MongoDB

不问归期 提交于 2019-12-03 02:48:20
问题 I want to use Spring security with MongoDB (using Spring data) and retrieve the users from my own database for spring security. However, I can not do that since my userservice type does not seem to be supported. This is my UserService class: public class UserService { private ApplicationContext applicationContext; private MongoOperations mongoOperations; public UserService() { applicationContext = new AnnotationConfigApplicationContext(MongoConfig.class); mongoOperations = (MongoOperations)

MongoDB query to capitalise first letter in existing database

无人久伴 提交于 2019-12-02 23:06:06
问题 Here is the problem, I want to capitalise the first letter of a name in my existing database, just wanted to know if there is any query so that i can make it possbile. what i want -- in my database so many names is in unformatted ways.. like lucy, Sean, jon and so on. i want to make them in a formatted ways like. Lucy. Sean, Jon. Can anyone help me with this? Thanks in advance. 回答1: it may not be the best solution. the only hiccup in below suggestion is to get "3" of $substr:["$name1",1, 3 ]

MongoDB java driver 3.0 can't catch exception when authenticate

倖福魔咒の 提交于 2019-12-02 21:34:49
问题 I'm super stuck o_0 While trying to authenticate via Java driver, there is an issue with catching exception. As you may see even Throwable class doesn't work private MongoClient mongoClient; private MongoDatabase mongoDatabase; public MongoConnection(String login, String password) { try { mongoClient = new MongoClient(asList(new ServerAddress("localhost"), new ServerAddress("localhost:27017")), singletonList(MongoCredential.createCredential(login, "cookbook", password.toCharArray()))); this

QueryBuilder and BasicDBObjectBuilder usage in MongoDB 3.3.0 above

你说的曾经没有我的故事 提交于 2019-12-02 18:18:54
问题 PART 1 Following up solutions to Querying Mongo Collection using QueryBuilder in Mongo 3.3.0 , I tried implementing the suggested methods to implement collection.find() . But was stuck with different parameters been passed a BasicDBObjectBuilder to the same as follows - BasicDBObjectBuilder queryBuilder = BasicDBObjectBuilder.start(); query.getParams().entrySet().stream().forEach(entry -> queryBuilder.add(entry.getKey(), entry.getValue())); BasicDBObjectBuilder outputQuery =

How to get mongo command results in to a flat file

你说的曾经没有我的故事 提交于 2019-12-02 15:45:44
How do I export the results of a MongoDB command to a flat file For example, If I am to get db.collectionname.find() into a flat file. I tried db.collectionname.find() >> "test.txt" doesnt seem to work. you can try the following from the command line mongo 127.0.0.1/db --eval "var c = db.collection.find(); while(c.hasNext()) {printjson(c.next())}" >> test.txt assuming you have a database called 'db' running on localhost and a collection called 'collection' this will export all records into a file called test.txt If you have a longer script that you want to execute you can also create a script

MongoDB query to capitalise first letter in existing database

感情迁移 提交于 2019-12-02 13:10:01
Here is the problem, I want to capitalise the first letter of a name in my existing database, just wanted to know if there is any query so that i can make it possbile. what i want -- in my database so many names is in unformatted ways.. like lucy, Sean, jon and so on. i want to make them in a formatted ways like. Lucy. Sean, Jon. Can anyone help me with this? Thanks in advance. it may not be the best solution. the only hiccup in below suggestion is to get "3" of $substr:["$name1",1, 3 ] dynamically. but gives you a start? db.toupper.aggregate([{$project:{name:{$concat:[{$toUpper:{$substr:["

MongoDB java driver 3.0 can't catch exception when authenticate

假装没事ソ 提交于 2019-12-02 09:27:33
I'm super stuck o_0 While trying to authenticate via Java driver, there is an issue with catching exception. As you may see even Throwable class doesn't work private MongoClient mongoClient; private MongoDatabase mongoDatabase; public MongoConnection(String login, String password) { try { mongoClient = new MongoClient(asList(new ServerAddress("localhost"), new ServerAddress("localhost:27017")), singletonList(MongoCredential.createCredential(login, "cookbook", password.toCharArray()))); this.mongoDatabase = mongoClient.getDatabase("cookbook"); } catch (Throwable e) { System.out.println(

Get the number of open connections in mongoDB using java

让人想犯罪 __ 提交于 2019-12-02 04:13:48
问题 My program requires a large number of connections to be open (Mongo). I get the error : Too many connections open, can't open anymore after 819 connections. I already know we can increase this limit. But that's not what I have in mind. I'm thinking of closing the MongoClient object, and then creating a new one again after 800 connections. My thinking is that with a new mongoClient object all the connections will be closed and when I start/create it again, the connections will be opened again

MongoDB Slow query by ID

こ雲淡風輕ζ 提交于 2019-12-02 01:03:00
问题 I recently migrated my MongoDB database from Windows Server to CentOS. The version is 2.4.9. I noticed that I have a slow retrieval of records by _id field! I ran a repair database over the weekend bu that did not solve the problem. I have this method that retrieves records by their IDs (with IN operator) (using spring data mongodb 1.4.1.RELEASE): @Override public Map<String, Record> findAsMapIds( final String[] ids, final ComponentType... comps ) { if( null == ids || 0 == ids.length ) {