mongodb-query

Filter $lookup results

孤者浪人 提交于 2019-12-07 20:21:15
问题 I have 2 collections (with example documents): reports { id: "R1", type: "xyz", } reportfiles { id: "F1", reportid: "R1", time: ISODate("2016-06-13T14:20:25.812Z") }, { id: "F14", reportid: "R1", time: ISODate("2016-06-15T09:20:29.809Z") } As you can see one report may have multiple reportfiles . I'd like to perform a query, matching a report id , returning the report document as is, plus an additional key storing as subdocument the reportfile with the most recent time (even better without

MongoTemplate aggregate - group by date

孤街醉人 提交于 2019-12-07 20:13:08
问题 I'm trying to create an aggregate query using mongotemplate where there's a grouping by date (i.e 2016-03-01) instead of datetime (i.e 2016-03-01 16:40:12). The dateToString operation exists in the mongodb documentation it can be used to extract the date from the datetime using formatting: https://docs.mongodb.org/manual/reference/operator/aggregation/dateToString/ but I get get it to work with mongotemplate - I get a NullPointerException. (my db version is 3.2) List<AggregationOperation>

How can I work with “join” to export data from MongoDB?

房东的猫 提交于 2019-12-07 18:14:15
问题 I have 2 collections: list1 and list2. list1 have some fields and list2 have another fields, including the id referring to list1. I need to do a query to export all items on list1 that have at least one item referring to him on list2. How can I do this? It's something like a join from list1 to list2. I need to run a mongoexport command to generate a csv file. 回答1: The way I do this is to create a short javascript program that will transfer the data you want to export into a new temporary

MongoDB get every second result

时间秒杀一切 提交于 2019-12-07 18:04:59
问题 In MongoDB all documents have a date field, it is a timestamp. There is a lot of data, and I want to get only some part of it, for every interval: e.g. 400ms 1402093316030<---- 1402093316123 1402093316223 1402093316400<---- 1402093316520 1402093316630 1402093316824<---- Is it possible to get every other, or every third result? Or better first document every 400 ms? 回答1: You can do this with the aggregation framework and a little date math. Let's say you have a "timestamp" field and addtional

MongoDB C++, How to add ISODate value when inserting

做~自己de王妃 提交于 2019-12-07 17:18:45
问题 This is about the new MongoDB C++ Driver (not the legacy one). I can insert a document this way: value Value = document{} <<"Key" <<"Value" <<finalize; cxxClient["db"]["collection"].insert_one(Value.view()); The above code insert a document with 1 field 'Key' of value 'Value'. I can insert string, int, float,... but just can't figure out how to insert ISODate. The new MongoDB C++ Driver should come with more examples in documentation. 回答1: Thanks Styvane, I found it out how! value Value =

MongoDB - Get the size of all the documents in a query

只愿长相守 提交于 2019-12-07 16:29:00
问题 Is there's a way to get the size of all the documents that meets a certain query in the MongoDB shell? I'm creating a tool that will use mongodump (see here) with the query option to dump specific data on an external media device. However, I would like to see if all the documents will fit in the external media device before starting the dump. That's why I would like to get the size of all the documents that meets the query. I am aware of the Object.bsonsize method described here, but it seems

Mongo / Mongoose Aggregation - $redact and $cond issues

末鹿安然 提交于 2019-12-07 16:21:23
问题 I was fortunate enough to get an awesome answer to another SO question Mongo / Mongoose - Aggregating by Date from @chridam which given a set of documents like: { "_id" : ObjectId("5907a5850b459d4fdcdf49ac"), "amount" : -33.3, "name" : "RINGGO", "method" : "VIS", "date" : ISODate("2017-04-26T23:00:00Z"), "importDate" : ISODate("2017-05-01T21:15:49.581Z"), "category" : "Not Set", "__v" : 0 } { "_id" : ObjectId("5907a5850b459d4fdcdf49ba"), "amount" : -61.3, "name" : "Amazon", "method" : "VIS",

How can I solve MongoWaitQueueFullException?

谁都会走 提交于 2019-12-07 15:48:52
问题 I run a java program which is a thread executor program that inserts thousands of documents to a table in mongodb. I get the following error Exception in thread "pool-1-thread-301" com.mongodb.MongoWaitQueueFullException: Too many threads are already waiting for a connection. Max number of threads (maxWaitQueueSize) of 500 has been exceeded. at com.mongodb.PooledConnectionProvider.get(PooledConnectionProvider.java:70) at com.mongodb.DefaultServer.getConnection(DefaultServer.java:73) at com

Sort and Group in one MongoDB aggregation query

霸气de小男生 提交于 2019-12-07 15:37:59
问题 Using $sort and $group in one aggregation query behaving strangely. Test data: db.createCollection("test"); db.test.insert({ ts : 100, category : 1 }); db.test.insert({ ts : 80, category : 1 }); db.test.insert({ ts : 60, category : 2 }); db.test.insert({ ts : 40, category : 3 }); So when sorting it by ts all looks good, but when I use both $sort and $group result goes in a wrong order. Query: db.test.aggregate([ { $sort : {ts: 1} }, { $group:{"_id":"$category"} } ]); And the result in reverse

set default values to mongoose arrays in node js

跟風遠走 提交于 2019-12-07 15:33:14
问题 In my model definition, I have: appFeatures: [{ name: String, param : [{ name : String, value : String }] }] I want to set default value to appFeatures, for example: name: 'feature', param: [{name:'param1',value:'1'},{name:'param2',value:'2'}] I tried to do it by appFeatures : { type : Array , "default" : ... } But its not working, any ideas? Thanks 回答1: Mongoose allows you to "separate" schema definitions. Both for general "re-use" and clarity of code. So a better way to do this is: //