mongo-java

Partition data around a match query during aggregation

你。 提交于 2021-02-05 11:18:30
问题 What I have been trying to get my head around is to perform some kind of partitioning(split by predicate) in a mongo query. My current query looks like: db.posts.aggregate([ {"$match": { $and:[ {$or:[{"toggled":false},{"toggled":true, "status":"INACTIVE"}]} , {"updatedAt":{$gte:1549786260000}} ] }}, {"$unwind" :"$interests"}, {"$group" : {"_id": {"iid": "$interests", "pid":"$publisher"}, "count": {"$sum" : 1}}}, {"$project":{ _id: 0, "iid": "$_id.iid", "pid": "$_id.pid", "count": 1 }} ]) This

Java MongoDB save multiple documents at once

帅比萌擦擦* 提交于 2020-06-08 11:15:40
问题 I Have a list of updated objects/documents i need save all the objects in the list at once. I saw save() in MongoTemplate but it can take single document at a time. Is there any way to save multiple documents at once or i need to call save in loop ? 回答1: Thanks for all the help. I was able to do it using Spring data MongoDB. Spring data MongoDB's MongoRepository has many inbuilt methods. org.springframework.data.mongodb.repository.MongoRepository.saveAll(Iterable entites) is the one which i

Mongo Database save data from Map

别等时光非礼了梦想. 提交于 2020-01-24 05:13:06
问题 I have the below code which works: if (aDBCursor.hasNext()) { DBObject aDbObject = aDBCursor.next(); aDbObject.put("title", "Test Title"); ArrayList<DBObject> department = new ArrayList<DBObject>(); DBObject nested1 = new BasicDBObject(); nested1.put("name", "Department A"); nested1.put("id", 1); department.add(nested1); DBObject nested2 = new BasicDBObject(); nested2.put("name", "Department B"); nested2.put("id", 2); department.add(nested2); aDbObject.put("department", department);

Executing Mongo like Query (JSON)through Java

巧了我就是萌 提交于 2020-01-20 16:58:09
问题 I was wondering if there is a way to execute mongo like query directly through Java i.e. we give mongoDB like query as a String to a function in Java driver for mongoDB as a String Object and an DBCursor Object is returned. Something like: import com.mongodb.*; import java.net.UnknownHostException; public class ExecuteQuery { public static void main(String args[]){ try{ Mongo m = new Mongo(); DB db = m.getDB("test"); DBCollection coll = db.getCollection("first"); DBObject doc = new

Executing Mongo like Query (JSON)through Java

雨燕双飞 提交于 2020-01-20 16:58:07
问题 I was wondering if there is a way to execute mongo like query directly through Java i.e. we give mongoDB like query as a String to a function in Java driver for mongoDB as a String Object and an DBCursor Object is returned. Something like: import com.mongodb.*; import java.net.UnknownHostException; public class ExecuteQuery { public static void main(String args[]){ try{ Mongo m = new Mongo(); DB db = m.getDB("test"); DBCollection coll = db.getCollection("first"); DBObject doc = new

Parse sql query using antlr parsetree to mongo bson document in Java

纵然是瞬间 提交于 2020-01-14 05:37:11
问题 I have a SQL like query example: Select id,name from employee where age > 30 and department = 'IT' limit 200 The SQL query grammer is defined in an ANTLR4 grammar file. Is there any implementation that converts the parse tree of this query to a bson document? The bson document will then be used to query a mongo db. 回答1: In one of my previous jobs I did something similar: got a query (not an sql, but pretty similar) and translated it to mongo query with antlr. I don't have a code to share,

Unable to update Inner Arraylist object using Mongodb Java Driver

拈花ヽ惹草 提交于 2020-01-06 14:14:31
问题 I have below document structure in mongodb database: { "_id" : ObjectId("52ec7b43e4b048cd48499b35"), "eidlist" : [ { "eid" : "64286", "dst" : NumberLong(21044), "score" : 0 }, { "eid" : "65077", "dst" : NumberLong(21044), "score" : 0 } ], "src" : NumberLong(21047) } I would like to update score field of first object using Java-mongodb driver: I tried following code but it is not working :( : DBObject update_query=new BasicDBObject("src", key).append("eidlist.eid", e.getEdgeid()); DBObject

How to create Decimal128 field with inc operator in java/scala

笑着哭i 提交于 2020-01-04 05:40:08
问题 I have following document structure: { "moneys": { "someKey": NumberDecimal(99) ... "someOtherRandomKey": NumberDecimal(99) } { What I want : When nonexistent field increments, create that field with NumberDecimal value. I tried it with scala driver but cant do that: //not compiles collection.findOneAndUpdate(filters,Updates.inc("someOtherKey", new Decimal128(50))) because Updates.inc(k,v) requires Number ; Decimal128 is not Number I think problem not in driver, but with my logic. How can I

java code gives error in mongodb while compiling

给你一囗甜甜゛ 提交于 2020-01-02 07:10:13
问题 I am new to mongodb and i have the following code import com.mongodb.*; import com.mongodb.Block; import com.mongodb.client.AggregateIterable; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import org.bson.types.ObjectId; import static java.util.Arrays.asList; public class getAssets{ public static void main( String args[] ){ Block<Document> printBlock = new Block<Document>() { @Override public void apply(final Document document) {

Bson pretty print using Java MongoDb driver

我怕爱的太早我们不能终老 提交于 2019-12-30 07:22:09
问题 I am using the Mongo Aggregation Framework using the Java MongoDB driver, version 3.3. I have an aggregagtion pipeline , that is merely collection of type List<Bson> . I am trying to find a way to pretty print each stage of the pipeline. Calling the toString method on each element is not sufficient, because each stages is an instance of a simple implementation of the Bson interface, which is SimplePipelineStage . This stupid class has not any override of the toString method. The pipeline is