在日期时间的月、日、年...上查询 Mongodb

Posted

技术标签:

?????????????????...??? Mongodb??????:Query Mongodb on month, day, year... of a datetime ??????:2011-12-29 12:22:13 ??????:

????? mongodb,????????????????????

????“17-11-2011 18:00”???:

date = datetime.datetime(2011, 11, 17, 18, 0)
db.mydatabase.mycollection.insert("date" : date)

????????

month = 11
db.mydatabase.mycollection.find("date.month" : month)

?

day = 17
db.mydatabase.mycollection.find("date.day" : day)

?????????????

??????:

?????????????,??????????????(??????? cmets)???,DrColossos ??????????????......??????? ??????????????????????????????? MongoDB ???,?????????????????????,???????????????(???????????????) )???????????,????????,?????(??????)?????? ?????1?:

????????????????????????????,????????????

var start = new Date(2010, 11, 1);
var end = new Date(2010, 11, 30);

db.posts.find(created_on: $gte: start, $lt: end);
//taken from http://cookbook.mongodb.org/patterns/date_range/

????:

?????????????????????????????????? @DrColossos,??????? $where ??? @DrColossos,??????????.. $where ??????????,??????????????... ????end ?????,??????? 31 ???????,month=11 ? 12 ?,??? 11 ?? Op ????? Nov. ?,????????...???????????,??????????....?????28?, 29 ??30 ?? 31 ?……?????????????:??: $year: 2014, $month:4 ????????,??????????2?:

?????????????????? mongodb ?????????????? $where javascript ???

db.mydatabase.mycollection.find($where : function()  return this.date.getMonth() == 11 )

????

db.mydatabase.mycollection.find($where : 'return this.date.getMonth() == 11')

(????????)

???? shell ?????????

>date = ISODate("2011-09-25T10:12:34Z")
> date.getYear()
111
> date.getMonth()
8
> date.getdate()
25

??:

??????????? $where?????????????@kamaradclimber ?@dcrosta ?????cmets??????????,????????????

????$where Clauses and Functions in Queries??????

????:

?????????,?????????????,??????? $where :????????????@DrColossos ??????????? @kamaradclimber,??,??,???????????,??????????????????..???..?????????????????.... ?????????????????????????:mongodb.org/display/DOCS/…????? db.eval ??????????,????????:-) DrColossos ????????? ????????,???mongod ??????javascript ????????javascript($where?map-reduce ???)????????,?????????????????,???????? javascript ????????????$where ?????????3?:

?????????,????????????????????$where ??,???????,?????????

????:

???,???????(AKA index ^^)??????4?:

???????????????,????????:

// Anything greater than this month and less than the next month
db.posts.find(created_on: $gte: new Date(2015, 6, 1), $lt: new Date(2015, 7, 1));

?????????????

// This may not find document with date as the last date of the month
db.posts.find(created_on: $gte: new Date(2015, 6, 1), $lt: new Date(2015, 6, 30));

// don't do this too
db.posts.find(created_on: $gte: new Date(2015, 6, 1), $lte: new Date(2015, 6, 30));

????:

?????????????????????????????????????????????5?:

????????????,?$dayOfYear?$dayOfWeek?$month?$year???????????????

??,?????? 2016 ? 4 ????????,??????????

db.getCollection('orders').aggregate(
   [
     
       $project:
         
           doc: "$$ROOT",
           year:  $year: "$created" ,
           month:  $month: "$created" ,
           day:  $dayOfMonth: "$created" 
         
     ,
      $match :  "month" : 4, "year": 2016  
   ]
)

????????????????,$$ROOT ?????????????????????,????????????????

?????????????,???????????????????????,???the link?

????:

???????????????????,??????? @Punnet Singh,??,?????? createdAt ?????????????????????????6?:

?????MongoDB_DataObject wrapper ???????:

$model = new MongoDB_DataObject('orders');

$model->whereAdd('MONTH(created) = 4 AND YEAR(created) = 2016');

$model->find();

while ($model->fetch()) 
    var_dump($model);

??,???,?????????:

$model = new MongoDB_DataObject();

$model->query('SELECT * FROM orders WHERE MONTH(created) = 4 AND YEAR(created) = 2016');

while ($model->fetch()) 
    var_dump($model);

????:

?????7?:

??$expr ???,?????????????????????????????Date Aggregation Operators,????:

month = 11
db.mydatabase.mycollection.find( 
    "$expr":  
        "$eq": [  "$month": "$date" , month ] 
     
)

?

day = 17
db.mydatabase.mycollection.find( 
    "$expr":  
        "$eq": [  "$dayOfMonth": "$date" , day ] 
     
)

??????aggregate() ????????,?????$redact ??:

month = 11
db.mydatabase.mycollection.aggregate([
    
        "$redact": 
            "$cond": [
                 "$eq": [  "$month": "$date" , month ] ,
                "$$KEEP",
                "$$PRUNE"
            ]
        
    
])

????

day = 17
db.mydatabase.mycollection.aggregate([
    
        "$redact": 
            "$cond": [
                 "$eq": [  "$dayOfMonth": "$date" , day ] ,
                "$$KEEP",
                "$$PRUNE"
            ]
        
    
])

?? OR

month = 11
day = 17
db.mydatabase.mycollection.aggregate([
    
        "$redact": 
            "$cond": [
                 
                    "$or": [ 
                         "$eq": [  "$month": "$date" , month ] ,
                         "$eq": [  "$dayOfMonth": "$date" , day ] 
                    ] 
                ,
                "$$KEEP",
                "$$PRUNE"
            ]
        
    
])

???

var month = 11,
    day = 17;
db.collection.aggregate([
    
        "$redact": 
            "$cond": [
                 
                    "$and": [ 
                         "$eq": [  "$month": "$createdAt" , month ] ,
                         "$eq": [  "$dayOfMonth": "$createdAt" , day ] 
                    ] 
                ,
                "$$KEEP",
                "$$PRUNE"
            ]
        
    
])

$redact ?????? $project ? $match ?????,???????????????$$KEEP ??? $$PRUNE ???????????????

????:

以上是关于在日期时间的月、日、年...上查询 Mongodb的主要内容,如果未能解决你的问题,请参考以下文章

查询两个日期相差的月数和剩下的天数

在飞镖中添加/减去迄今为止的月/年?

按日期时间的月/日订购 Django QuerySet?

将两个给定日期之间的月、年和日计算为时间戳[重复]

如何在查询语句 Mysql 中格式化日期?

如何计算两个日期之间的月数和天数,而不在 SQL Server 中返回负值