mongodb-java

How to create query with ObjectIds using java?

五迷三道 提交于 2020-01-15 07:42:20
问题 I have set of ids like: ["51eae104c2e6b6c222ec3432", "51eae104c2e6b6c222ec3432", "51eae104c2e6b6c222ec3432"] I need to find all documents using this set of ids. BasicDBObject query = new BasicDBObject(); BasicDBList list = new BasicDBList(); ObjectId ob1 = new ObjectId("51eae100c2e6b6c222ec3431"); ObjectId ob2 = new ObjectId("51eae100c2e6b6c222ec3432"); list.add(ob1); list.add(ob2); query.append("_id", new BasicDBObject("$in", list)); This query can't find anything because it is same as { "

Index already exists with different options error while using createIndex() in latest MongoDB java driver

左心房为你撑大大i 提交于 2020-01-14 07:55:12
问题 So I am upgrading the MongoDB java driver to 2.12.4 where the ensureIndex() method has been deprecated. I am instead using the createIndex() method which from the docs seems like to funciton similarly to ensureIndex() . However, when I use this method in production, I get the following error - { "serverUsed" : "X.X.X.X" , "ok" : 0.0 , "errmsg" : "Index with name: <index_name> already exists with different options" , "code" : 85} Why does this happen? Could anyone help me out with this? Thanks

updating a value in an array in mongodb from java

懵懂的女人 提交于 2020-01-14 06:07:02
问题 I have couple of documens in mongodb as follow: { "_id" : ObjectId("54901212f315dce7077204af"), "Date" : ISODate("2014-10-20T04:00:00.000Z"), "Type" : "Twitter", "Entities" : [ { "ID" : 4, "Name" : "test1", "Sentiment" : { "Value" : 20, "Neutral" : 1 } }, { "ID" : 5, "Name" : "test5", "Sentiment" : { "Value" : 10, "Neutral" : 1 } } ] } Now I want to update the document that has Entities.ID=4 by adding (Sentiment.Value+4)/2 for example in the above example after update we have 12. I wrote the

updating a value in an array in mongodb from java

拈花ヽ惹草 提交于 2020-01-14 06:06:33
问题 I have couple of documens in mongodb as follow: { "_id" : ObjectId("54901212f315dce7077204af"), "Date" : ISODate("2014-10-20T04:00:00.000Z"), "Type" : "Twitter", "Entities" : [ { "ID" : 4, "Name" : "test1", "Sentiment" : { "Value" : 20, "Neutral" : 1 } }, { "ID" : 5, "Name" : "test5", "Sentiment" : { "Value" : 10, "Neutral" : 1 } } ] } Now I want to update the document that has Entities.ID=4 by adding (Sentiment.Value+4)/2 for example in the above example after update we have 12. I wrote the

Conversion from String to MongoDB ObjectID

泄露秘密 提交于 2020-01-11 10:14:13
问题 I tried converting my String ID to MongoDB ObjectID public class relevancy_test extends Object implements Comparable<ObjectId> { public static void main(String[] args) throws UnknownHostException { MongoClient mongo = new MongoClient("localhost", 27017); DB mydb = mongo.getDB("test"); DBCollection mycoll = mydb.getCollection("mytempcoll"); BasicDBObject query = null; Map<ObjectId, DBObject> updateMap = new HashMap<ObjectId, DBObject>(); List<DBObject> dbobj = null; DBCursor cursor = mycoll

Conversion from String to MongoDB ObjectID

好久不见. 提交于 2020-01-11 10:13:03
问题 I tried converting my String ID to MongoDB ObjectID public class relevancy_test extends Object implements Comparable<ObjectId> { public static void main(String[] args) throws UnknownHostException { MongoClient mongo = new MongoClient("localhost", 27017); DB mydb = mongo.getDB("test"); DBCollection mycoll = mydb.getCollection("mytempcoll"); BasicDBObject query = null; Map<ObjectId, DBObject> updateMap = new HashMap<ObjectId, DBObject>(); List<DBObject> dbobj = null; DBCursor cursor = mycoll

Call stored function in mongodb

一个人想着一个人 提交于 2020-01-09 10:13:49
问题 I'm getting some difficulties to call a stored function within mongdb. I'm a little bit newbie with mongo. [EDIT] : my function stored in mongodb function() { var cndTime = new Date().getTime() - (3600*1000*24*2); // condition db.urls.find(needParse: false).forEach(function(item){ if(item.date < cndTime) // check db.urls.update({_id: item._id}, {$set: { needParse: true }}); // update field }); } All I'm asking is how to invoke this function using reactivemongo or native API. 回答1: Consider the

Call stored function in mongodb

蹲街弑〆低调 提交于 2020-01-09 10:12:08
问题 I'm getting some difficulties to call a stored function within mongdb. I'm a little bit newbie with mongo. [EDIT] : my function stored in mongodb function() { var cndTime = new Date().getTime() - (3600*1000*24*2); // condition db.urls.find(needParse: false).forEach(function(item){ if(item.date < cndTime) // check db.urls.update({_id: item._id}, {$set: { needParse: true }}); // update field }); } All I'm asking is how to invoke this function using reactivemongo or native API. 回答1: Consider the

Concurrent update to the same collection same document

非 Y 不嫁゛ 提交于 2020-01-05 18:37:36
问题 Problem Statement How do i handle concurrent update commands on same collection and same document. Current DB Structure EDIT - Structure changed { "userId":1, "summary_extra":[ { "some_id":1, "names":[ "one", "two" ] }, { "some_id":2, "names":[ "three", "four" ] } ] }, { "userId":2, "summary_extra":[ { "some_id":1, "names":[ "one", "two" ] }, { "some_id":2, "names":[ "three", "four" ] } ] } If one user tries to update at a time then there wont be any problem to update the document. But if

Concurrent update to the same collection same document

北慕城南 提交于 2020-01-05 18:37:19
问题 Problem Statement How do i handle concurrent update commands on same collection and same document. Current DB Structure EDIT - Structure changed { "userId":1, "summary_extra":[ { "some_id":1, "names":[ "one", "two" ] }, { "some_id":2, "names":[ "three", "four" ] } ] }, { "userId":2, "summary_extra":[ { "some_id":1, "names":[ "one", "two" ] }, { "some_id":2, "names":[ "three", "four" ] } ] } If one user tries to update at a time then there wont be any problem to update the document. But if