mongodb-query

Mongodb : Why convert string date to ISOdate if comparison operators work?

心不动则不痛 提交于 2019-12-23 15:42:57
问题 I have the following kind of document: { "_id" : ObjectId("538d64a11ca6e50941fda4d9"), "_id" : "538d518e20b8fd642e0000e8", "posts" : "some stuff", "date" : "2014-06-02" } Using comparison operators for a string date (Not the Mongodb ISODate) works: > collection.find({"date": {"$gte": "2014-06-02"}}) So why shall we (bother to) convert string dates to an ISODate then? 回答1: Probably the biggest advantage of using the MongoDB BSON Date type instead of a string is that you can only use the

mongoDB query for retrieving from nested array collection

被刻印的时光 ゝ 提交于 2019-12-23 13:28:33
问题 { "_id" : ObjectId("576155a6cd87b68f7e6e42c9"), "First_Name" : "ok", "Last_Name" : "jaao", "Email" : "xyz@gmail.com", "Sessions" : [ { "Last_Login" : "Wed, Jun 14, 2016 6:48 PM", "Class" : "fb", "ID" : "123" }, { "Last_Login" : "Wed, Jun 15, 2016 6:48 PM", "ID" : "111", "Class" : "fb" } ], "Count" : 2 }, { "_id" : ObjectId("576155ccf6d8979e7e77df27"), "First_Name" : "abc", "Last_Name" : "xyz", "Email" : "xyz@gmail.com", "Sessions" : [ { "Last_Login" : "Wed, Jun 15, 2016 6:49 PM", "Class" :

Why are coordinates of polygon GeoJSON Objects stored in an array of array?

五迷三道 提交于 2019-12-23 12:41:15
问题 As seen in the Official documentation page, "Schema" of the polygon GeoJSON Object is as below: db.someCollection.insert({ type: "Polygon", coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ] }); why cannot it be simply as shown below: Type A db.someCollection.insert({ type: "Polygon", coordinates: [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] }); I assume the reason might be to store multiple geofences....am i right? Something like: Type B db.someCollection.insert({ type:

$unwind 2 fields separately in mongodb query

孤街浪徒 提交于 2019-12-23 12:26:37
问题 I want to $unwind 2 fields, school and home . database structure is like; { "id" : 1, "school" : [ { "path" : "school1", "code" : "code1", }, { "path" : "school2", "code" : "code2", }, { "path" : "school3", "code" : "code3", }, { "path" : "school4", "code" : "code4", } ], "home" : [ { "path" : "home1", "code" : "homeCode1", }, { "path" : "home2", "code" : "homeCode2", }, ] } I wanted to $unwind school and home fields and get each of them as; { "id" : 1, "school" : [ { "path" : "school1",

MongoDB Aggregate for a sum on a per week basis for all prior weeks

倖福魔咒の 提交于 2019-12-23 12:00:02
问题 I've got a series of docs in MongoDB. An example doc would be { createdAt: Mon Oct 12 2015 09:45:20 GMT-0700 (PDT), year: 2015, week: 41 } Imagine these span all weeks of the year and there can be many in the same week. I want to aggregate them in such a way that the resulting values are a sum of each week and all its prior weeks counting the total docs. So if there were something like 10 in the first week of the year and 20 in the second, the result could be something like [{ week: 1, total:

MongoDB Aggregate for a sum on a per week basis for all prior weeks

…衆ロ難τιáo~ 提交于 2019-12-23 11:59:28
问题 I've got a series of docs in MongoDB. An example doc would be { createdAt: Mon Oct 12 2015 09:45:20 GMT-0700 (PDT), year: 2015, week: 41 } Imagine these span all weeks of the year and there can be many in the same week. I want to aggregate them in such a way that the resulting values are a sum of each week and all its prior weeks counting the total docs. So if there were something like 10 in the first week of the year and 20 in the second, the result could be something like [{ week: 1, total:

mongo $sum compounded when doing $unwind and then $group on multiple fields

随声附和 提交于 2019-12-23 10:07:03
问题 I have the following document structure { "app_id": "DHJFK67JDSJjdasj909", "date": ISODate("2014-08-07T00:00:00.000Z"), "event_count": 100, "events": [ { "type": 0, "value": 12 }, { "type": 10, "value": 24 }, { "type": 20, "value": 36 }, { "type": 30, "value": 43 } ], "unique_events": [ { "type": 0, "value": 5 }, { "type": 10, "value": 8 }, { "type": 20, "value": 12 }, { "type": 30, "value": 56 } ] } I am trying to get a sum of event_counts and also the values for unique_events and events per

MongoDB: Updating an average in a document with 2 nested arrays

谁都会走 提交于 2019-12-23 09:42:04
问题 I have the following MongoDB document: { _id: ObjectId(), company_name: "Name", registered: 2/21/2015 2:00, trucks: [ { truck_id: "TEB7622", weight: 88.33, capacity: 273.333, length: 378.333, width: 377.383, average_grade: 2.5, grades: [ { grade_number: 4, timestamp: 2/21/2015 2:00 } ] }, { truck_id: "TEB5572", weight: 854.33, capacity: 2735.333, length: 378.333, width: 37.383, average_grade: 3.8, grades: [ { grade_number: 4, timestamp: 2/21/2015 2:00 } ] } ] } I want to update each truck's

The Field “$_id” must be an accumulator object

旧巷老猫 提交于 2019-12-23 08:48:16
问题 I am trying to group some documents within mongoDB, right after a $match stage like this: db.trips.aggregate([ { "$match": { "Stop Time": { "$lt": "31.07.2013 23:59" } } }, { "$group": { "$_id": { "Start": "$Start", "End": "$End" }, } }] However I am getting the following error: "The field '$_id' must be an accumulator object" Why does this error show up, and why does it only show up when I include the match stage? Without the match stage, it works just fine. 回答1: $_id is not an accumulator

The Field “$_id” must be an accumulator object

我的未来我决定 提交于 2019-12-23 08:46:02
问题 I am trying to group some documents within mongoDB, right after a $match stage like this: db.trips.aggregate([ { "$match": { "Stop Time": { "$lt": "31.07.2013 23:59" } } }, { "$group": { "$_id": { "Start": "$Start", "End": "$End" }, } }] However I am getting the following error: "The field '$_id' must be an accumulator object" Why does this error show up, and why does it only show up when I include the match stage? Without the match stage, it works just fine. 回答1: $_id is not an accumulator