mongodb-query

MongoDB aggregation - $group by date even if doesn't exist

本秂侑毒 提交于 2019-12-06 05:44:38
问题 I have a query already which looks like this: {$match:{ "when":{$gt: new Date(ISODate().getTime() - 1000 * 60 * 60 * 24 * 30)} }}, {$project:{ "year":{$year:"$when"}, "month":{$month:"$when"}, "day": {$dayOfMonth:"$when"} }}, {$group:{ _id:{year:"$year", month:"$month", day:"$day"}, "count":{$sum:1} }}, {$sort:{ _id: 1 }} The result looks like this: { "_id" : { "year" : 2015, "month" : 10, "day" : 19 }, "count" : 1 } { "_id" : { "year" : 2015, "month" : 10, "day" : 21 }, "count" : 2 } How

$lookup nested array in mongodb

无人久伴 提交于 2019-12-06 05:35:11
问题 I am struggling with the newish (lovely) lookup operator in MongoDB. I have 3 collections: artists { "_id" : ObjectId("5b0d2b2c7ac4792df69a9942"), "name" : "Dream Theater", "started_in" : NumberInt(1985), "active" : true, "country" : "US", "current_members" : [ ObjectId("5b0d2a7c7ac4792df69a9941") ], "previous_members" : [ ObjectId("5b0d2bf57ac4792df69a9954") ], "albums" : [ ObjectId("5b0d16ee7ac4792df69a9924"), ObjectId("5b0d47667ac4792df69a9994") ], "genres" : [ "prog metal", "prog rock" ]

MongoDB $group and explicit group formation with computed column

别来无恙 提交于 2019-12-06 05:33:01
Consider this code in SQL that forms groups based on a range of salary: ;With TableWithComputedColumn as ( select computedcolumn = Case when Salary<30000 then 'Under 30K' when Salary>30000 and Salary<60000 then '30K-60K' else 'Above 60K' end from Tbl_UserMaster ) select computedcolumn, COUNT(*) from TableWithComputedColumn group by computedcolumn I want to do this in Mongo.. My guess is that the CTE part will require me to first output {computed column} to a temporary collection and then do a $group on that collection. Another way (and the one I prefer) could be to use $project to project

Iterating through PyMongo cursor throws InvalidBSON: year is out of range

空扰寡人 提交于 2019-12-06 05:28:21
I am using PyMongo to simply iterate over a Mongo collection, but I'm struggling with handling large Mongodb date objects. For example, if I have some data in a collection that looks like this: "bad_data" : [ { "id" : "id01", "label" : "bad_data", "value" : "exist", "type" : "String", "lastModified" : ISODate("2018-06-01T10:04:35.000Z"), "expires" : Date(9223372036854775000) } ] I will do something like: from pymongo import MongoClient, database, cursor, collection client = MongoClient('localhost') db = client['db1'] db.authenticate('user', 'pass', source='admin') collection = db['collection']

mongodb array matching

随声附和 提交于 2019-12-06 05:26:35
How do I accomplish the following? db.test.save( {a: [1,2,3]} ); db.test.find( {a: [1,2,3,4]} ); //must match because it contains all required values [1, 2, 3] db.test.find( {a: [1,2]} ); //must NOT match because the required value 3 is missing I know about $in and $all but they work differently. Interesting..The problem is..the $in and the $or operators get applied on the elements of the array that you are comparing against each document in the collection, not on the elements of the arrays in the documents..To summarize your question: You want it to be a match, if any of the documents in the

How to search comma separated data in mongodb

巧了我就是萌 提交于 2019-12-06 04:22:46
I have movie database with different fields. the Genre field contains a comma separated string like : {genre: 'Action, Adventure, Sci-Fi'} I know I can use regular expression to find the matches. I also tried: {'genre': {'$in': genre}} the problem is the running time. it take lot of time to return a query result. the database has about 300K documents and I have done normal indexing over 'genre' field. Would say use Map-Reduce to create a separate collection that stores the genre as an array with values coming from the split comma separated string, which you can then run the Map-Reduce job and

Mongo and Node.js: Finding a document by _id using a UUID (GUID)

这一生的挚爱 提交于 2019-12-06 04:18:52
I'm developing a restAPI using node.js and i'm trying to query a mongo collection. I can query using strings (for example "companyname") but I need to be able to query the "_id" element in the document. In mongo the _id is currently stored like this (as a GUID): { "_id" : new BinData(3, "MH+t3q6PD0SxVR5z7/pzfw=="), "companyname" : "TestCompany", "databasename" : "TestDataBase", } This is what my current GET method looks like: exports.getBusinessCardData = function(req, res) { var id = req.params.id; //"id" = MH+t3q6PD0SxVR5z7/pzfw== tradeShowAdminDB.collection('clients', function(err,

how to convert timestamp to date in mongodb?

一笑奈何 提交于 2019-12-06 03:47:17
问题 i am working on aggregation of mongodb collection.my mongodb collection has creation_time in timestamp.How will i can collect same day result and aggregate with next day result.Suppose i have to collect data for 5 days.My mongodb collection is: { "_id" : "1522845653126" }, { "_id" : "1522838153324" }, { "_id" : "1513421466415" }, { "_id" : "1515488183153" }, { "_id" : "1521571234500" } How can i calculate.How many entry are save on specific date?? nd suppose I want is to query results that

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

末鹿安然 提交于 2019-12-06 03:24:41
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 that it only returns the size of one document. Here's the answer that I've found: var cursor = db

Aggregate $sum values from two arrays

无人久伴 提交于 2019-12-06 03:04:34
问题 I have this collection { "_id" : ObjectId("54f46f18c36dcc206d0cec38"), "project" : 23123, "title" : "Change of windows", "description": "Change to better windows on building A" "costs":[ { category: 'Produktionskostnad', value: 3000 }, { category: 'Projekteringskostnad', value: 2000 }, { category: 'Overhead', value: 1000 } ], "energySaving" : [ { "energy" : "electricity", "type" : "lighting", "value" : 24324 }, { "energy" : "electricity", "type" : "equipment", "value" : 24324 }, { "energy" :