MongoDB + nodejs:如何查询ISODate字段?

时间:2013-12-13 07:37:27

标签: node.js mongodb isodate

我将nodejs与node-mongodb-native驱动程序(http://mongodb.github.io/node-mongodb-native/)一起使用。

我有一个日期属性存储为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')
    }
})

但我的nodejs代码中无法识别ISODate

那么如何通过我的nodejs程序对mongo日期字段进行查询?

谢谢

4 个答案:

答案 0 :(得分:56)

您可以在new Date('2013-12-12T16:00:00.000Z');

中使用node.js

new 是必须的,因为Date()已用于返回日期字符串。

ISODate在mongodb中被隐藏,你可以在mongodb控制台中使用它,但它可以用于不同的编程语言。

答案 1 :(得分:4)

你可以使用它,因为我工作得很好

//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){ 

        }

      }
    });


  }

});

答案 2 :(得分: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"
      }
    }
  }
])

答案 3 :(得分:0)

您可以浏览我的回答(在其他询问的问题中提供)。

使用$ gte:“ yyyy-mm-dd”,$ lte:“ yyyy-mm-dd”

NodeJS MongoDB: Querying ISO Date

希望这对您或其他人有帮助!