mongodb-java

MongoDB Java driver : no such cmd: aggregate

北慕城南 提交于 2020-01-05 05:50:10
问题 I am calling the MongoDB aggregate function in my code as : AggregationOutput output = collection.aggregate( matchUserID, unwindF, matchFUsers,projection); I have tested my code in my localhost, and it works perfect. When I am using the same in another DB (version 2.2.1), it gives this error : com.mongodb.CommandResult$CommandFailure: command failed [aggregate]: { "serverUsed" : "<server address>" , "errmsg" : "no such cmd: aggregate" , "bad cmd" : { "aggregate" : .... } Any clue why ? 回答1:

Java MongoClient cannot connect to primary

拟墨画扇 提交于 2020-01-03 06:44:12
问题 I have a replica set setup with 1 primary (mongo1.test.com), 1 secondary(mongo2.test.com) and 1 arbiter(mongo3.test.com). When I use MongoClient to connect to them and print out the ReplicaSetStatus, it shows that mongo1.test.com is unconnected with type=Unknown, mongo2.test.com as type=ReplicaSetSecondary, and mongo3.test.com as type=Unknown. Because it doesn't know which one is the primary, I can do find queries but I cannot insert or update. Right now I don't know if it is the setup with

Getting the required documents from both the collections in one query in MongoDB-3.2.7

瘦欲@ 提交于 2020-01-03 04:25:52
问题 I have two collections like below in MongoDB. lookupcol1 { "_id" : 1, "sku" : "abc", description: "product 1", "instock" : 120, "groupId":100100}, { "_id" : 2, "sku" : "def", description: "product 2", "instock" : 80,"groupId":100100 }, { "_id" : 3, "sku" : "ijk", description: "product 3", "instock" : 60 }, { "_id" : 4, "sku" : "jkl",description: "product 4", "instock" : 70 }, { "_id" : 5, "sku": null, description: "Incomplete", "groupId":100100 }, { "_id" : 6 } lookupcol2 { "_id" : 12, "sku"

$near query through Java

旧时模样 提交于 2020-01-03 01:47:08
问题 I want to run a $near query on a collection through Java. I am not sure how to use QeuryBuilder or BasicDbObject for this. What is the correct way to run $near query through Java code. Below is my code for document structure. "location" attribute stores type as point and coordinates stores the lat-long. I have created a 2dsphere index on this collection. BasicDBObject doc = new BasicDBObject("attr1", nextLine[0]) .append("attr2", nextLine[1]) .append("edge-metro-code", nextLine[6]) .append(

Restrict fields in Result

倾然丶 夕夏残阳落幕 提交于 2020-01-02 12:13:26
问题 I am working with MongoDB v3.0.1 and the MongoDB Java Driver 3.0.0-RC1. I have an user collection with fields like "username", "firstname", "lastname", "email", and so on. Now I want to select all users but only with the fields "username", "firstname" and "lastname". On the Mongo-Shell it is working with db.user.find({}, { "username" : true , "firstname" : true , "lastname" : true}) But how can I do it in Java? I tried it with final BasicDBObject query = new BasicDBObject("{}", new

How to write multiple group by id fields in Mongodb java driver

家住魔仙堡 提交于 2020-01-02 04:08:28
问题 In the below query { $group : { _id : { success:'$success', responseCode:'$responseCode', label:'$label'}, max_timeStamp : { $timeStamp : 1 }, count_responseCode : { $sum : 1 }, avg_value : { $sum : "$value" }, count_success : { $sum : 1 } }} How _id : { success:'$success', responseCode:'$responseCode', label:'$label'}, can be translated to use in java mongodb driver. I tried BasicDBList list = new BasicDBList(); list.add(new BasicDBObject("success", "$success")); list.add(new BasicDBObject(

how to pivot data using spring-data-mongodb

笑着哭i 提交于 2020-01-01 07:21:10
问题 I have data of this form and I referred to this link pivoting in mongodb: Name, SumPrice, type,year A, 50.0, Retail,2015 B, 467.0, Online,2015 A, 1456.0, Direct,2015 B, 1149.53, Direct,2015 C, 273.20, Online,2014 C, 110.0, Direct,2014 I want data to be transformed using spring-data-mongodb of this form Name ,Retail, Online , Direct, year A, 50.0 , 0.0, 1456.0 2015 B, 0.0, 467.0, 1149.53, 2015 C, 0.0, 273.20, 110.0, 2014 Please lemme know how to do. Regards kris 来源: https://stackoverflow.com

Spring-Data mongodb querying multiple classes stored in the same collection

。_饼干妹妹 提交于 2020-01-01 03:23:10
问题 With Spring-Data, you can use the @Document annotation to specify which collection to save the object to. Say I have two classes, Student and Teacher, both persisted into the people collection. When I execute the following code: mongo.find(new Query(), Teacher.class); the result contains both Student and Teacher. Looking in the data created by Spring-Data, each document contains a "_class" field which indicate which class it is persisted from. This is field not used in find as an filter to

Check MongoDB server is running and credentials are valid in Java

て烟熏妆下的殇ゞ 提交于 2020-01-01 01:14:14
问题 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

MongoDB+Azure+Android: com.mongodb.WriteConcernException err: “not master” code: “10058”

六月ゝ 毕业季﹏ 提交于 2019-12-31 03:49:10
问题 Background : Hi, I am running a MongoDB replica set on Azure and have connected to it remotely from within an Android app. I've gotten reads to work great from all instances (Updated: Because they are allowed to read on primary and secondary nodes). But, writes to the database still have intermittent errors with the following error, because writes must be done on the primary node only. Also, if you could provide any more specific resources for dealing with this issue, then that would be very