2013-12-13 89 views
28

我正在使用nodejs和node-mongodb-native驅動程序(http://mongodb.github.io/node-mongodb-native/)。MongoDB + nodejs:如何查詢ISODate字段?

我有一個日期屬性存儲爲ISODate類型的文檔。

通過的NodeJS,我使用此查詢:

db.collection("log").find({ 
    localHitDate: { 
      '$gte': '2013-12-12T16:00:00.000Z', 
      '$lt': '2013-12-12T18:00:00.000Z' 
    } 
}) 

它沒有返回。爲了使它工作,我需要做的,而不是執行以下操作:

db.collection("log").find({ 
    localHitDate: { 
      '$gte': ISODate('2013-12-12T16:00:00.000Z'), 
      '$lt': ISODate('2013-12-12T18:00:00.000Z') 
    } 
}) 

ISODate是不是在我的代碼的NodeJS認可。

那麼如何通過我的nodejs程序對mongo日期字段進行查詢?

謝謝

回答

40

可以在node.js使用new Date('2013-12-12T16:00:00.000Z');

new是必須的,因爲Date()已經用來返回日期字符串。

ISODate包含在mongodb中,您可以在mongodb控制檯中使用它,但對於不同的編程語言它可能會有所不同。

+0

IM真的有一個超級困難的時候,這個......我似乎無法存儲UTC isodate在蒙戈救我一命...... http://stackoverflow.com/questions/26874993/javascript-momentjs -convert-UTC-從串最新對象 – Cmag

3

你可以使用這個,對我的工作完美

//lets require/import the mongodb native drivers. 
var mongodb = require('mongodb'); 

//We need to work with "MongoClient" interface in order to connect to a mongodb server. 
var MongoClient = mongodb.MongoClient; 

// Connection URL. This is where your mongodb server is running. 
var url = 'mongodb://localhost/klevin'; 

// Use connect method to connect to the Server 
MongoClient.connect(url, function (err, db) { 

    if (err) { 
    console.log('Unable to connect to the mongoDB server. Error:', err); 
    } else { 
    //HURRAY!! We are connected. :) 
    console.log('Connection established to', url); 


    // Get the documents collection 
    var collection = db.collection('frames'); 

    //We have a cursor now with our find criteria 
    var cursor = collection.find({ 
     tv: 'tematv', 
     date_created: {"$gte": new Date("2015-10-01T00:00:00.000Z") , "$lt": new Date("2017-03-13T16:17:36.470Z") }}); 

    //We need to sort by age descending 
    cursor.sort({_id: -1}); 

    //Limit to max 10 records 
    cursor.limit(50); 

    //Skip specified records. 0 for skipping 0 records. 
    cursor.skip(0); 


    //Lets iterate on the result 
    cursor.each(function (err, doc) { 

     if (err) { 

     console.log(err); 

     } else { 

     console.log('Fetched:', doc); 

     if(doc !== null){ 

     } 

     } 
    }); 


    } 

}); 
0

我們需要使用新的Date()是獲取數據的最佳選擇。

db.getCollection('orders').aggregate([ 
    { 
    '$match': { 
     $and: [ 
     { 
      status: 'UNASSIGNED' 
     }, 
     { 
      plannedDeliveryDate: { 
      '$eq': new Date('2017-10-09') 
      } 
     } 
     ] 
    } 
    }, 
    { 
    $lookup: { 
     from: "servicelocations", 
     localField: "serviceLocationId", 
     foreignField: "serviceLocationId", 
     as: "locations" 
    } 
    }, 
    { 
    $unwind: "$locations" 
    }, 
    { 
    "$project": { 
     "accountId": 1, 
     "orderId": 1, 
     "serviceLocationId": 1, 
     "orderDate": 1, 
     "description": 1, 
     "serviceType": 1, 
     "orderSource": 1, 
     "takenBy": 1, 
     "plannedDeliveryDate": 1, 
     "plannedDeliveryTime": 1, 
     "actualDeliveryDate": 1, 
     "actualDeliveryTime": 1, 
     "deliveredBy": 1, 
     "size1": 1, 
     "size2": 1, 
     "size3": 1, 
     "jobPriority": 1, 
     "cancelReason": 1, 
     "cancelDate": 1, 
     "cancelBy": 1, 
     "reasonCode": 1, 
     "reasonText": 1, 
     "status": 1, 
     "lineItems": 1, 
     "locations": { 
     "lng": "$locations.location.lng", 
     "lat": "$locations.location.lat" 
     } 
    } 
    } 
])