按月和按年分组,包括NULL

时间:2017-03-02 16:47:58

标签: mongodb

我试图按日期对所有条目进行分组,并将NULL分组到自己的组中,但我没有正确的语法。

我试过了

 ---> Running in 63b3db498b39
Running via Spring preloader in process 19
rake aborted!                                                                                                                                                                                         
StandardError: An error has occurred, this and all later migrations canceled:                                                                                                                         

SQLite3::SQLException: no such table: produits: ALTER TABLE "produits" ADD "id_category" integer/usr/local/bundle/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `initialize'                      
/usr/local/bundle/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `new'                                                                                                                             
/usr/local/bundle/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `prepare'                                                                                                                         
/usr/local/bundle/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:137:in `execute'                                                                                                                        
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:329:in `block in execute'                                                                          
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'                                                                             
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'                                                                                        
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'                                                                                      
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:329:in `execute'                                                                                   
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:393:in `add_column'                                                                     
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:453:in `add_column'                                                                                
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:662:in `block in method_missing'                                                                                             
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:632:in `block in say_with_time'                                                                                              
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:632:in `say_with_time'                                                                                                       
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:652:in `method_missing'                                                                                                      
/db/migrate/20150507055752_add_id_category.rb:3:in `change'                                                                                                                                           
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:606:in `exec_migration'                                                                                                      
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'                                                                                         
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:589:in `block in migrate'                                                                                                    
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'                                                                  
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:588:in `migrate'                                                                                                             
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:765:in `migrate'                                                                                                             
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:995:in `block in execute_migration_in_transaction'                                                                           
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:1041:in `block in ddl_transaction'                                                                                           
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'                                                         
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'                                                               
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'                                                                  
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/transactions.rb:220:in `transaction'                                                                                                      
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:1041:in `ddl_transaction'                                                                                                    
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'                                                                                    
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:956:in `block in migrate'                                                                                                    
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:952:in `each'                                                                                                                
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:952:in `migrate'                                                                                                             
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:820:in `up'                                                                                                                  
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:798:in `migrate'                                                                                                             
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:137:in `migrate'                                                                                                  
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'                                                                      
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'                                                                                                           
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'                                                                                                  
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'                                                                                                
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'                                                                                                           
-e:1:in `<main>'                                                                                                                                                                                      
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: produits: ALTER TABLE "produits" ADD "id_category" integer                                                                      
/usr/local/bundle/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `initialize'                                                                                                                      
/usr/local/bundle/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `new'                                                                                                                             
/usr/local/bundle/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `prepare'                                                                                                                         
/usr/local/bundle/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:137:in `execute'                                                                                                                        
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:329:in `block in execute'                                                                          
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'                                                                             
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'                                                                                        
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'                                                                                      
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:329:in `execute'                                                                                   
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:393:in `add_column'                                                                     
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:453:in `add_column'                                                                                
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:662:in `block in method_missing'                                                                                             
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:632:in `block in say_with_time'                                                                                              
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:632:in `say_with_time'                                                                                                       
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:652:in `method_missing'                                                                                                      
/db/migrate/20150507055752_add_id_category.rb:3:in `change'                                                                                                                                           
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:606:in `exec_migration'                                                                                                      
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'                                                                                         
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:589:in `block in migrate'                                                                                                    
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'                                                                  
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:588:in `migrate'                                                                                                             
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:765:in `migrate'                                                                                                             
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:995:in `block in execute_migration_in_transaction'                                                                           
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:1041:in `block in ddl_transaction'                                                                                           
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'                                                         
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'                                                               
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'                                                                  
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/transactions.rb:220:in `transaction'                                                                                                      
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:1041:in `ddl_transaction'                                                                                                    
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'                                                                                    
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:956:in `block in migrate'                                                                                                    
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:952:in `each'                                                                                                                
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:952:in `migrate'                                                                                                             
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:820:in `up'                                                                                                                  
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:798:in `migrate'                                                                                                             
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
-e:1:in `<main>'
== 20150501094406 CreateCategories: migrating =================================
-- create_table(:categories)
   -> 0.0024s
== 20150501094406 CreateCategories: migrated (0.0026s) ========================

== 20150507055752 AddIdCategory: migrating ====================================
-- add_column(:produits, :id_category, :integer)
SQLite3::SQLException: no such table: produits
/usr/local/bundle/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `initialize'
/usr/local/bundle/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `new'
/usr/local/bundle/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `prepare'
/usr/local/bundle/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:137:in `execute'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:329:in `block in execute'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:329:in `execute'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:393:in `add_column'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:453:in `add_column'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:662:in `block in method_missing'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:632:in `block in say_with_time'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:632:in `say_with_time'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:652:in `method_missing'
/db/migrate/20150507055752_add_id_category.rb:3:in `change'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:606:in `exec_migration'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:589:in `block in migrate'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:588:in `migrate'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:765:in `migrate'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:995:in `block in execute_migration_in_transaction'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:1041:in `block in ddl_transaction'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/transactions.rb:220:in `transaction'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:1041:in `ddl_transaction'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:956:in `block in migrate'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:952:in `each'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:952:in `migrate'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:820:in `up'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/migration.rb:798:in `migrate'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/usr/local/bundle/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/usr/local/bundle/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
ERROR: Service 'ecommerce' failed to build: The command '/bin/sh -c bin/rake db:migrate' returned a non-zero code: 1

这会在元素列表&#39;之后给出“缺失”。我因为$ IfNull中的$ year而猜测。

也尝试了

db.DateCollection.aggregate({$project:{year:{$ifNull:[$year:"$received-date","NULL"]},month:{$ifNull:[$month:"$received-date","NULL"]}}},{$group:{_id:{year:"$year",month:"$month"},count:{$sum:1}}});

这样就无法将BSON类型的NULL转换为Date&#39;猜测因为它无法将NULL转换为日期。

0 个答案:

没有答案