mongodb-java

dbref command in mongodb

情到浓时终转凉″ 提交于 2019-12-11 01:14:06
问题 I am newbie to mongodb. I need to do a query by reference two collections using dbref and need to query particular fields. comments collection { uid:12345, pid:444, comment="blah" }, { uid:12345, pid:888, comment="asdf" }, { uid:99999, pid:444, comment="qwer" } users collection { uid:12345, name:"john" }, { uid:99999, name:"mia" } May I know the command how to insert these collections in mongodb by adding reference between two collections usind dbref? comments in uid must refer the users uid.

Query failed with error code 13 and error message 'not authorized on [db] to execute command { find:

断了今生、忘了曾经 提交于 2019-12-11 00:11:51
问题 I am using the following Mongo 3.2.7 Spring 4.3.1 release I have created following user with the below privileges. > use admin switched to db admin > db.getUser("UserAdminNew") { "_id" : "admin.UserAdminNew", "user" : "UserAdminNew", "db" : "admin", "roles" : [ { "role" : "readWrite", "db" : "admin" }, { "role" : "root", "db" : "admin" }, { "role" : "readWrite", "db" : "NEWTest" } ] } In DB NEWTest I have the below mentioned collections > use NEWTest switched to db NEWTest > show collections

spring-data-mongodb giving mongodb authentication exception

柔情痞子 提交于 2019-12-10 19:16:00
问题 I am using Mongodb 3.2.0 Mongo-Java-Driver 3.2.0 spring-data-mongodb 1.9.2 and referred to this link accessing-mongodb-with-authentication, and created user in myDb by logging as admin to admin database. db.createUser({user: "admin",pwd: "password",roles: [ { role: "readWrite", db: "myDb" } ]}); db.auth('admin','password'); db.grantRolesToUser("admin",[{ role: "dbAdmin", db: "myDb" }]) for setting up mongodb authentication the link example worked. The spring-data configuration is : <bean

Trying to create a mongodb collection in grails using mongodb plugin

吃可爱长大的小学妹 提交于 2019-12-10 19:11:33
问题 I am trying to create a mongodb collection in grails using: BasicDBObject DBoptions = new BasicDBObject(); DBCollection collection = db.createCollection("xyz",DBoptions) But I get the following error: Caused by GroovyRuntimeException: Ambiguous method overloading for method com.mongodb.DBApiLayer#createCollection. Cannot resolve which method to invoke for [class java.lang.String, class com.mongodb.BasicDBObject] due to overlapping prototypes between: [class java.lang.String, interface com

Java MongoDB getting value for sub document

自古美人都是妖i 提交于 2019-12-10 14:17:26
问题 I am trying to get the value of a key from a sub-document and I can't seem to figure out how to use the BasicDBObject.get() function since the key is embedded two levels deep. Here is the structure of the document File { name: file_1 report: { name: report_1, group: RnD } } Basically a file has multiple reports and I need to retrieve the names of all reports in a given file. I am able to do BasicDBObject.get("name") and I can get the value "file_1", but how do I do something like this

MongoDB Java Driver: MongoCore Driver vs. MongoDB Driver vs. MongoDB Async Driver

僤鯓⒐⒋嵵緔 提交于 2019-12-10 13:55:48
问题 There are three different driver options for the MongoDB Java driver: Core Driver MongoDB Driver MongoDB Async Driver The drivers description page gives a brief description of each of them but no further explanation is provided regarding when I should use each of them. My question: can you, please, clarify what are the cases to use each of them? When should I prefer one over the second and when I must/have to use the particular driver option? 回答1: TL;DR : Use the async driver if the

Distinct() command used with skip() and limit()

左心房为你撑大大i 提交于 2019-12-10 13:19:17
问题 I have those items in my MongoDB collection: {x: 1, y: 60, z:100} {x: 1, y: 60, z:100} {x: 1, y: 60, z:100} {x: 2, y: 60, z:100} {x: 2, y: 60, z:100} {x: 3, y: 60, z:100} {x: 4, y: 60, z:100} {x: 4, y: 60, z:100} {x: 5, y: 60, z:100} {x: 6, y: 60, z:100} {x: 6, y: 60, z:100} {x: 6, y: 60, z:100} {x: 7, y: 60, z:100} {x: 7, y: 60, z:100} I want to query the distinct values of x (i.e. [1, 2, 3, 4, 5, 6, 7] ) ... but I only want a part of them (similar to what we can obtain with skip(a) and

Converting DBObject to Java Object while retrieve values from MongoDB

时光毁灭记忆、已成空白 提交于 2019-12-09 10:12:00
问题 From my Java application, I have stored the values in mongoDB in ArrayList(set of Java objects). How can I retrieve the data from DBObject I am storing the data in mongoDB like this: { "students" : [{"firstName" : "Jesse", "lastName" : "Varnell", "age" : "15", "gender" : "M" }, { "firstName" : "John", "lastName" : "Doe", "age" : "13", "gender" : "F"}] } I am having the Java Object for the Student like: public class Student { public String firstName; public String lastName; public String age;

Mongo DB query in java

假如想象 提交于 2019-12-09 08:39:21
问题 I have to write a complex mongo query using java but am not able to do it. The mongo query looks like this: db.video.findOne( { $or: [ { key1: { $in : [764] } }, { key2: {$in : [list2] } }, { $and [ { key2 : 3}, {key4:67} ] } ] }) I have to write the above query using the QueryBuilder class. In what way can I do it? Thanks 回答1: Using QueryBuilder your query should look like this DBObject query = QueryBuilder.start().or( QueryBuilder.start("key1").in(764).get(), QueryBuilder.start("key2").in

Bulk insert performance in MongoDB for large collections

微笑、不失礼 提交于 2019-12-09 05:14:00
问题 I'm using the BulkWriteOperation (java driver) to store data in large chunks. At first it seems to be working fine, but when the collection grows in size, the inserts can take quite a lot of time. Currently for a collection of 20M documents, bulk insert of 1000 documents could take about 10 seconds. Is there a way to make inserts independent of collection size? I don't have any updates or upserts, it's always new data I'm inserting. Judging from the log, there doesn't seem to be any issue