answerstu

Apache Camel read MongoDB Collection - not processing any rows

I have the following Camel setup reading from MongoDB and writing to files, but it's not working:public static void main(String args[]) throws Exception { // create CamelContext SimpleRegistry sr = new SimpleRegistry(); Mongo mongo = new Mongo("localhost", 27017); sr.put("mdb", mongo); CamelContext context = new DefaultCamelContext(sr); context.addRoutes(new RouteBuilder() { public void configure() { from("mongodb:mdb?database=demo&collection=person").to("file:data/outbox"); } }); // start th...Read more

MongoDB Scala - query document for a specific field value

So I know that in Mongo Shell, you use dot notation to get the field you want in any document.How is dot notation achieved in MongoDB Scala. I'm confused as to how it works. Here is the code that fetches a document from a collection:val record = collection.find().projection(fields(include("offset"), excludeId())).limit(1)EDIT:I'm trying to work on a mechanism to basically re-consume Kafka records at a point where the consumer was shutdown. To do this, I store my kafka records in an external database, and then try to fetch the most recent offset...Read more

mongodb - Using Mongo aggregation to calculate sum of values

I have invoices, where each invoice contains a list of items. Each item has (among other things) the following fields:nameqtytotalAnd each invoice has (among other things) the fields:_idcreateditemsThe invoices live in a dedicated Mongo collection, called invoices.I would like to get all the invoices containing the specified item, where for each invoice the following information needs to be returned:_idcreatedqty (of the given item)total (of the given item)Let us call the tuple <id, date, qty, total> as the invoice projection. Thus the re...Read more

mongoid - MongoDB Aggregate Query Group By

Say I have a MongoDB collection containing the following information:{ cust_id: "abc123", ord_date: ISODate("2012-11-02T17:04:11.102Z"), state: 'CA', price: 50, item: apple, color: red}{ cust_id: "abc123", ord_date: ISODate("2012-11-02T17:04:11.102Z"), state: 'WA', price: 25, item: apple, color: green}{ cust_id: "abc123", ord_date: ISODate("2012-11-02T17:04:11.102Z"), state: 'CA', price: 75, item: orange, color: orange}{ cust_id: "def456", ord_date: ISODate("2012-11-02T17:04:11.102Z"), state: 'OR', price: 75, item: apple,...Read more

Mongodb aggregation $group followed by $limit for pagination

In MongoDB aggregation pipeline, record flow from stage to stage happens one/batch at a time (or) will wait for the current stage to complete for whole collection before passing it to next stage?For e.g., I have a collection classtest with following sample records{name: "Person1", marks: 20}{name: "Person2", marks: 20}{name: "Person1", marks: 20}I have total 1000 records for about 100 students and I have following aggregate query db.classtest.aggregate([ {$sort: {name: 1}}, {$group: {_id: '$name', total: {$sum: '$marks'}}}, ...Read more

mongodb - How to join two collection in mongo without lookup

I have two collection, there name are post and comment.The model structure is in the following.I want to use aggregation query post and sort by comments like length sum, currently I can query a post comments like length sum in the following query statement. My question is how can I query post and join comment collection in Mongo version 2.6. I know after Mongo 3.2 have a lookup function. I want to query post collection and sort by foreign comments likes length. Is it have a best way to do this in mongo 2.6? post{ "_id": ObjectId("5a39e22c273...Read more

mongoose - Mongodb $lookup Aggregation Query takes 11+ seconds, How do I optimise this

I have a stock price alerts application with 3 collections1) Alerts{ "_id" : ObjectId("5a9543c235434a185038e778"), //alert id "1" : ObjectId("5a9543c035434a185038e743"), //user id "2" : "xyz", //asset id "3" : "EUR", //fiat set by the user "4" : NumberDecimal("1000"), //price at which user wants alert "5" : true, // true if 1000 was greater than price at the time of setting the alert "6" : 0, //type 0 indicating it is a price alert "__v" : 0}2) Asset value in USD which updates every 2 mins, has 2000 assets currently{ ...Read more

mongodb - mongo sort by group criteria

I have data structure like this (mongo 2.6):{"player":"John Doe","active":true}lets say I use the term "ohn" when performing search, I need to sort based on certain criteria:players whose name is "ohn" AND activeplayers whose name CONTAINS "ohn" AND activeplayers whose name is "ohn" but NOT activeplayers whose name CONTAINS "ohn" but NOT activehow do I achieve this in mongo 2.6?the expected output is all players who meets any of those 4 criteria, but sorted in that order (players who meet crit 1, then players who meet crit 2, and so on). I also...Read more

Mongodb Aggregation Pipeline limit $lookup fields

I have two collections that i am trying to aggregate together to produce the below desired output but am struggling with how to limit the fields in the array returned by the lookup. Any help would be greatly appreciated.project{ _id: ObjectId("ABC") projectName: "Project One"}user{ _id: ObjectId("...") email: "test1@test.co.uk", firstname: "Test1", lastname: "Record", project: [{projectId: ObjectId("ABC")}]},{ _id: ObjectId("...") email: "test2@test.co.uk", firstname: "Test2", lastname: "Record", project...Read more

mongodb - Mongo DB aggregation not supping ISO date conversion

in below mention query when I am trying to use project1 "finish" and "planFinish" to project2 and converting "String" date to "IsoDate" its showing "QUERY [thread1] Error: invalid ISO date "db.prOrder.aggregate([{$match:{"enterpriseBid":"abc","prodOrderLineItems.planFinishDate":{$exists:true}}},//Stage2{$unwind:{path:"$prodOrderLineItems",preserveNullAndEmptyArrays:false}},//Stage3{$project:{prodOrderNumber:1,enterpriseBid:1, "planFinish":{$dateToString:{format:"%Y-%m-%d",date:"$prodOrderLineItems.planFinishDate"}}, "finish":{$dateToString...Read more

performance - Mongodb query on all second level documents if any of sub document satisfy condition

I am trying to get default_billing from address book in following second level sub document{ "_id" : ObjectId("5a841ac387c7d70ad36f5ce2"), "user_type" : "retail", "first_name" : "Mayank", "last_name" : "Garg", "addressbook" : { "5a93d64187c7d71562433a22" : { "consignee_name" : "Jerry Day", "first_name" : "Cole", "last_name" : "Bean", "mobile" : "33333333333333", "street_address" : "", "street_address2...Read more

spring mvc - Migrate Collections and documents from one MongoDB to another

** Suppose there are two mongoDb installed in two deployment location: The first deployment location is server 1 and second server is Remote Desktop: I know to import and export data using one collection at a time. Is there any way, So that i can export whole DB to JSON and import that JSON at once into another DB . DB1: students ( 20 collections) DB2: Student2: Migrate all collections from Students to Student2 **...Read more